如何将 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:08:35  来源:igfitidea点击:

How to locate JLabels to an absolute position on Java GUI

javauser-interfaceswinglayout-manager

提问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.

  1. Set the container's layout manager to null by calling setLayout(null).
  2. Call the Componentclass's setboundsmethod for each of the container's children.
  3. Call the Componentclass's repaintmethod.

创建没有布局管理器的容器包括以下步骤。

  1. 通过调用将容器的布局管理器设置为 null setLayout(null)
  2. 为每个容器的子Component级调用类的setbounds方法。
  3. 调用Component类的repaint方法。

回答by Tom Hawtin - tackline

Either

任何一个

回答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.

就布局管理器而言,这是非常好的,可以进行绝对定位,再加上一大堆其他事情。