JLabel setLocation不起作用? [英] JLabel setLocation not working?

查看:124
本文介绍了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不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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