问题
I have many JLabels (which includes ImageIcons) 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?
回答1:
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
Componentclass'ssetboundsmethod for each of the container's children.- Call the
Componentclass'srepaintmethod.
回答2:
Either
- set a custom LayoutManager (Container.setLayout) on the panel that sets the exact positions you want or
- set a
nulllayout manager (myPanel.setLayout(null);) and set component positions externally (Component.setBounds).
回答3:
Take a look at MigLayout, which allows you to do absolute positioning, it's not part of the JDK, but a seperate download.
It's very good as far as layout managers go and does absolute positioning, plus a whole load of other things.
来源:https://stackoverflow.com/questions/869245/how-to-locate-jlabels-to-an-absolute-position-on-java-gui