JLabel setLocation not working?

依然范特西╮ 提交于 2019-12-17 21:13:12

问题


Here is the code I have written:

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);
}

回答1:


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.

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.



来源:https://stackoverflow.com/questions/7264923/jlabel-setlocation-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!