Java - set the exact position of a JComponent

徘徊边缘 提交于 2021-02-10 18:25:35

问题


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

Thank you for your time.


回答1:


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

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.



来源:https://stackoverflow.com/questions/11894088/java-set-the-exact-position-of-a-jcomponent

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