Java-设置JComponent的确切位置 [英] Java - set the exact position of a JComponent

查看:78
本文介绍了Java-设置JComponent的确切位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置JPanel上JLabel的x和y位置时,我需要帮助,不是北,南,西,东.

I need help with setting the x and y position of a JLabel on a JPanel, not North,South,West,East.

谢谢您的时间.

推荐答案

要使JPanel尊重确切的位置,应将其布局设置为null.然后,您可以在任何JComponent上使用 setLocation 来设置其在该JPanel中的位置.

To have a JPanel respect exact locations, you should set its layout to null. Then, you can use setLocation on any JComponent to set its location within that JPanel.

JPanel panel = new JPanel();
panel.setLayout(null);
JLabel label = new JLabel("text");
label.setLocation(50, 20);
panel.add(label);

但是,请注意,绝对位置有几个缺点(在下面的注释中提到),并且应该利用LayoutManager来定位组件.

However, note that there are several downsides of absolute position (mentioned in comments below), and that you should utilize a LayoutManager to position your components.

这篇关于Java-设置JComponent的确切位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆