jcomponent

Drawing in JPanel vs JComponent

淺唱寂寞╮ 提交于 2019-12-01 09:58:50
问题 I need some help understanding why the drawing works differently in JComponent vs JPanel. import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; public class Particle extends JComponent implements Runnable{ private int x = 45; private int y = 45; private int cx; private int cy; private int size; private Color color; private JFrame frame; public Color getColor(){ return color = new Color

How do i recalculate preferred size of a JComponent?

扶醉桌前 提交于 2019-12-01 05:59:10
I know the fact that when I create an instance of a JComponent, it has it's own preferred size. Now let's suppose that I setPreferredSize that JComponent manually with a dimension of 0 x 0. And i want that Component to "reset" its own preferredSize. How do I do that? Tormod 1) Setting preferred size to null should reset the component back to getting its preferred size calculated as if it was never set. component.setPreferredSize(null); This might not do what you want, depending on how you signal that the layout should be redone - but at least it is technically the answer to your question. 2)

How to center text and a JComponent in a JTextPane vertically?

为君一笑 提交于 2019-12-01 04:38:52
问题 Currently it looks so What to do so that it looks so? Below is my code: JFrame f = new JFrame(); JTextPane textPane = new JTextPane(); JTextField component = new JTextField(" "); component.setMaximumSize(component.getPreferredSize()); textPane.setSelectionStart(textPane.getDocument().getLength()); textPane.setSelectionEnd(textPane.getDocument().getLength()); textPane.insertComponent(component); try { textPane.getDocument().insertString(textPane.getDocument().getLength(), "text", new

How do i recalculate preferred size of a JComponent?

北城以北 提交于 2019-12-01 04:18:45
问题 I know the fact that when I create an instance of a JComponent, it has it's own preferred size. Now let's suppose that I setPreferredSize that JComponent manually with a dimension of 0 x 0. And i want that Component to "reset" its own preferredSize. How do I do that? 回答1: 1) Setting preferred size to null should reset the component back to getting its preferred size calculated as if it was never set. component.setPreferredSize(null); This might not do what you want, depending on how you

Difference between JComponent.isShowing() and isDisplayable()

牧云@^-^@ 提交于 2019-12-01 03:23:07
What's the difference between Component.isShowing() and Component.isDisplayable() ? I want to use them to decide wheter I should stop/start a Timer. Pr0gr4mm3r A component isShowing() when Determines whether this component is showing on screen. This means that the component must be visible, and it must be in a container that is visible and showing. isShowing() is recursive and checks all parent components, too, but isDisplayable() and isVisible() verify only the state of the component, not that state of its parents. This means that your component is currently showing on the screen within a

Difference between JComponent.isShowing() and isDisplayable()

為{幸葍}努か 提交于 2019-11-30 23:23:50
问题 What's the difference between Component.isShowing() and Component.isDisplayable()? I want to use them to decide wheter I should stop/start a Timer. 回答1: A component isShowing() when Determines whether this component is showing on screen. This means that the component must be visible, and it must be in a container that is visible and showing. isShowing() is recursive and checks all parent components, too, but isDisplayable() and isVisible() verify only the state of the component, not that

What are the benefits to painting on a JPanel vs. JComponent?

烂漫一生 提交于 2019-11-29 18:23:14
问题 So in a recent answer, someone commented this (in regards to painting): "This is probably some kind of illness of 90% of Swing Programmers: When they make their own component, they always extend JPanel instead of JComponent. Why?" I'm still fairly new to programming, so I think it's too early to call myself a Swing programmer, as I have yet to find my niche. But overriding JPanel is just the way I was taught. So I set out to find the answer to the "Why?" question of the commenter. These are

Ball Animation in Swing

老子叫甜甜 提交于 2019-11-29 14:40:35
The Problem is when I resize the JFrame the animation goes on with it's Pre supplied dimensions for JComponent . Is there a way that I can update my width and height variables as I resize the JFrame , so that the Animation can run along with the new Co-ordinates. In simpler terms, say the JComponent has initial width = 300 and height = 300 , so the BALL moves inside these specified Co-ordinates . Now if I resize my JFrame , the size for the JComponent still remains as is i.e. width = 300 and height = 300 , but what I was hoping for, is a way to modify these variables with the present size of

Easier way to make a paint application in java?

时间秒杀一切 提交于 2019-11-29 08:59:58
So basically I have some code I was working on a couple of days ago that is kind of like Paint, which allows you to essentially draw on the screen using the mouse. I kind of discovered this property by accident, and I realized that it is really inefficient and i'm wondering if there is a more practical way to do this. There isn't really any reason to give all of my code, but here are the important parts private static void createAndShowGui() { SimpleDraw mainPanel = new SimpleDraw(); MenuBar.createMenuBar(); JLabel label = new JLabel(); label.setText("Drawing prototype 0.0.1"); // label

JPanel not listening to key event when there is a child component with JButton on it

我怕爱的太早我们不能终老 提交于 2019-11-28 14:23:46
I'm working on a map editor for my college project. And I had a problem that the map panel is not listening key event while it should. This happens when I add a ToolBarPane (which extends JPanel) with JComponent such as JButton, JComboBox that implements ActionListener on it AND the map panel (which extends the JPanel) together on to the Frame (I used BorderLayout). I have System.out.println statement to test if the key press is received, and it's not printing, if I remove the ToolBar, the key listener works again, so is the mouseListenner is disabled just like the keyListener, which means I