java 在 JPanel 中手动定位 JComponent
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2820872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Manually position JComponent inside JPanel
提问by oletus
I want to programmatically move my JLabel to a specific location inside my JPanel. I have tried setLocation(int x, int y), but it doesn't work.
I am trying to not use any layout manager.
我想以编程方式将我的 JLabel 移动到我的 JPanel 内的特定位置。我试过了setLocation(int x, int y),但它不起作用。我试图不使用任何布局管理器。
回答by jjnguy
Here is a great tutorial on how to layout your components without using a layout manager.
这是一个关于如何在不使用布局管理器的情况下布局组件的很棒的教程。
http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html
http://java.sun.com/docs/books/tutorial/uiswing/layout/none.html
Creating a container without a layout manager involves the following steps.
- Set the container's layout manager to null by calling
setLayout(null).- Call the Component class's
setboundsmethod for each of the container's children.- Call the Component class's
repaintmethod.
创建没有布局管理器的容器包括以下步骤。
- 通过调用将容器的布局管理器设置为 null
setLayout(null)。setbounds为容器的每个子项调用 Component 类的方法。- 调用 Component 类的
repaint方法。

