JLabel setLocation not working?(JLabel setLocation 不起作用?)
问题描述
这是我写的代码:
super("Add contact");
setLayout(new FlowLayout());
IPAddress = new JLabel("IP Address");
IPAddress.setLocation(1000, 100);
ImageIcon ii=new ImageIcon(getClass().getResource("Add.png"));
JLabel image = new JLabel(ii);
image.setSize(100, 100);
image.setLocation(500, 100);
add(image);
add(IPAddress);
setSize(500,150);
}
推荐答案
没错.布局管理器负责根据布局管理器的规则设置组件的位置.因此,在您的情况下,FlowLayout 将覆盖组件的位置.
That is correct. The layout manager is responsible for setting the location of a component based on the rules of the layout manager. So in your case the FlowLayout will override the location of the component.
您不应该对组件的位置进行硬编码.如果有人使用小于 1024 X 768 的分辨率怎么办?该组件永远不会显示.
You should never hardcode the location of a component. What if somebody is using resolution less then 1024 X 768? The component will never show.
您也不应该设置组件的大小.每个组件都有一个首选尺寸.在带有图像的标签的情况下,首选大小将是图像的大小.
You should also never set the size of a component. Every component has a preferred size. In the case of label with the image, the preferred size will be the size of the image.
阅读布局管理器并使用适当的布局管理器或布局组合经理来实现您想要的布局.
Read up on Layout Managers and use the appropriate layout manager or combination of layout managers to achieve your desired layout.
这篇关于JLabel setLocation 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JLabel setLocation 不起作用?


- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01