jcomponent

Why can't I validate a JComponent?

大城市里の小女人 提交于 2019-12-02 01:35:38
From JavaDoc : public void validate() Validates this container and all of its subcomponents. Validating a container means laying out its subcomponents. That is what I want to do. With an as lightweight component as possible. But when I do this whith a JComponent a call to validate() doesn't make the component "valid". JComponent c = new JComponent() {}; System.out.println(c.isValid()); // false c.validate(); System.out.println(c.isValid()); // false Why can't I make a JComponent valid? Chris In the docs for isValid() it says: A component is valid when it is correctly sized and positioned

Do I actually call the paintComponent method I make when creating a rectangle in Java?

孤人 提交于 2019-12-02 00:04:11
问题 This is my current RectangleComponent class and I add it to a panel in my main JFrame but it never appears. I thought it wasn't drawing so I decided to call the paintComponent method in the Rectangle's constructor, and after sorting through 4-5 nullPointerExceptions, nothing has changed. I've read multiple guides on how to draw rectangles and I have seen multiple code examples, but I can never get the panels to work with more than one JComponent. If you could, please take a brief look at my

Do I actually call the paintComponent method I make when creating a rectangle in Java?

橙三吉。 提交于 2019-12-01 21:39:37
This is my current RectangleComponent class and I add it to a panel in my main JFrame but it never appears. I thought it wasn't drawing so I decided to call the paintComponent method in the Rectangle's constructor, and after sorting through 4-5 nullPointerExceptions, nothing has changed. I've read multiple guides on how to draw rectangles and I have seen multiple code examples, but I can never get the panels to work with more than one JComponent. If you could, please take a brief look at my code and see if you can devise a solution. Thank you for your time. Also listed is the Frame I call the

Manually position JComponent inside JPanel

帅比萌擦擦* 提交于 2019-12-01 16:49:10
I want to programmatically move my JLabel to a specific location inside my JPanel. I have tried setLocation(int x, int y) , but it doesn't work. I am trying to not use any layout manager. Here is a great tutorial on how to layout your components without using a layout manager. http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html Creating a container without a layout manager involves the following steps. Set the container's layout manager to null by calling setLayout(null) . Call the Component class's setbounds method for each of the container's children. Call the Component class's

How can I get all the components of a panel in Java Swing?

给你一囗甜甜゛ 提交于 2019-12-01 15:58:29
How can I get all the components of a panel in Java Swing? Is there any method like foreach in C# to deal all the child components of a JPanel? You may use the method getComponents : Component[] components = jpanel.getComponents(); 来源: https://stackoverflow.com/questions/15727661/how-can-i-get-all-the-components-of-a-panel-in-java-swing

How can I get all the components of a panel in Java Swing?

喜你入骨 提交于 2019-12-01 14:56:44
问题 How can I get all the components of a panel in Java Swing? Is there any method like foreach in C# to deal all the child components of a JPanel? 回答1: You may use the method getComponents: Component[] components = jpanel.getComponents(); 来源: https://stackoverflow.com/questions/15727661/how-can-i-get-all-the-components-of-a-panel-in-java-swing

Java Making objects move while buttons held

限于喜欢 提交于 2019-12-01 14:42:34
How do I make a JPanel move while a button is held down and stop when the button is released. I have tried using thread.start() with a Runnable and ways like that. I always run in to errors. Can anyone help me though? There are a number of important considerations that you need to take into consideration. Buttons aren't designed to work this way. They are designed to trigger and action event when they are clicked (pressed and released), so you can't use the normal action API. Lucky for us, there are other ways to determine the state of buttons. This example uses a ChangeListener on the

Drawing in JPanel vs JComponent

核能气质少年 提交于 2019-12-01 13:00:03
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(100,0,190); } public Particle(){ frame = new JFrame(); frame.setSize(400, 400); frame.setResizable

Java Making objects move while buttons held

纵然是瞬间 提交于 2019-12-01 12:58:32
问题 How do I make a JPanel move while a button is held down and stop when the button is released. I have tried using thread.start() with a Runnable and ways like that. I always run in to errors. Can anyone help me though? 回答1: There are a number of important considerations that you need to take into consideration. Buttons aren't designed to work this way. They are designed to trigger and action event when they are clicked (pressed and released), so you can't use the normal action API. Lucky for

Java Swing: Access panel components from another class

浪子不回头ぞ 提交于 2019-12-01 10:29:36
Hi i basically have two classes, one main and one just to separate the panels, just for code readability really. i have : public class Main{ public static void main (String args[]) { JFrame mainJFrame; mainJFrame = new JFrame(); //some other code here CenterPanel centerPanel = new CenterPanel(); centerPanel.renderPanel(); mainFrame.add(centerPanel.getGUI()); } } class CenterPanel{ JPanel center = new JPanel(); public void renderPanel(){ JButton enterButton = new JButton("enter"); JButton exitButton = new JButton("exit"); center.add(exitButton); center.add(enterButton); } public JComponent