I have many JLabel
s (which includes ImageIcon
s) in a JPanel
.
And this JPanel
is only a panel on the GUI; there are lots of other panels.
I want to place labels to the exact pixel coordinates on their JPanel
container.
How can I do that without using GroupLayout
?
For this go to app > res > layout > activity_main. xml file and change the Constraint Layout to Absolute Layout and add TextViews.
By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.
See Doing Without a Layout Manager (Absolute Positioning) in the Java tutorials.
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'ssetbounds
method for each of the container's children.- Call the
Component
class'srepaint
method.
Either
LayoutManager
(Container.setLayout
) on the panel that sets the exact positions you want ornull
layout manager (myPanel.setLayout(null);
) and set component positions externally (Component.setBounds
).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