如何将 JLabels 定位到 Java GUI 上的绝对位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/869245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to locate JLabels to an absolute position on Java GUI
提问by AhmetB - Google
I have many JLabels (which includes ImageIcons) in a JPanel.
我在 a 中有很多JLabels(包括ImageIcons)JPanel。
And this JPanelis only a panel on the GUI; there are lots of other panels.
这JPanel只是 GUI 上的一个面板;还有很多其他面板。
I want to place labels to the exact pixel coordinates on their JPanelcontainer.
我想将标签放置在其JPanel容器上的确切像素坐标上。
How can I do that without using GroupLayout?
我如何在不使用的情况下做到这一点GroupLayout?
回答by Michael Myers
See Doing Without a Layout Manager (Absolute Positioning)in the Java tutorials.
请参阅Java 教程中的Doing without a Layout Manager (Absolute Positioning)。
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.
创建没有布局管理器的容器包括以下步骤。
- 通过调用将容器的布局管理器设置为 null
setLayout(null)。- 为每个容器的子
Component级调用类的setbounds方法。- 调用
Component类的repaint方法。
回答by Tom Hawtin - tackline
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).
- 在面板上设置自定义
LayoutManager(Container.setLayout) 以设置您想要的确切位置或 - 设置
null布局管理器 (myPanel.setLayout(null);) 并在外部设置组件位置 (Component.setBounds)。
回答by Jon
Take a look at MigLayout, which allows you to do absolute positioning, it's not part of the JDK, but a seperate download.
看看 MigLayout,它允许你做绝对定位,它不是 JDK 的一部分,而是一个单独的下载。
It's very good as far as layout managers go and does absolute positioning, plus a whole load of other things.
就布局管理器而言,这是非常好的,可以进行绝对定位,再加上一大堆其他事情。

