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.
JPanel is a subclass of the JComponent, which is a child of the Container; therefore, JPanel is also considered a container. There are many methods used by JPanel, which it inherits from its parent classes.
The most common JPanel constructor has no parameters. This creates a panel with the default layout (FlowLayout). Call setLayout() to change the layout. JPanel p = new JPanel(); p.
To use JComponent, the procedure is usually as follows: create a subclass of JComponent; override the paintComponent() method to draw whatever graphics are required; override getPreferredSize(), getMinimumSize() and getMaximumSize() to make your new component "behave" properly.
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
repaint
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With