[英]How to locate JLabels to an absolute position on Java GUI
I have many JLabel
s (which includes ImageIcon
s) in a JPanel
. 我在JPanel
有很多JLabel
(包括ImageIcon
)。
And this JPanel
is only a panel on the GUI; 而这个JPanel
只是GUI上的一个面板; there are lots of other panels. 还有很多其他面板。
I want to place labels to the exact pixel coordinates on their JPanel
container. 我想将标签放在JPanel
容器上的精确像素坐标上。
How can I do that without using GroupLayout
? 如何在不使用GroupLayout
情况下执行此GroupLayout
?
See Doing Without a Layout Manager (Absolute Positioning) in the Java tutorials. 请参阅Java教程中的“不使用布局管理器(绝对定位) ”。
Creating a container without a layout manager involves the following steps. 在没有布局管理器的情况下创建容器涉及以下步骤。
- Set the container's layout manager to null by calling
setLayout(null)
. 通过调用setLayout(null)
将容器的布局管理器设置为null。- Call the
Component
class'ssetbounds
method for each of the container's children. 为每个容器的子setbounds
调用Component
类的setbounds
方法。- Call the
Component
class'srepaint
method. 调用Component
类的repaint
方法。
Either 或
LayoutManager
( Container.setLayout
) on the panel that sets the exact positions you want or 在面板上设置自定义LayoutManager
( Container.setLayout
),设置您想要的确切位置或 null
layout manager ( myPanel.setLayout(null);
) and set component positions externally ( Component.setBounds
). 设置一个null
布局管理器( myPanel.setLayout(null);
)并在外部设置组件位置( Component.setBounds
)。 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. 就布局管理人员而言,这是非常好的绝对定位,以及其他一些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.