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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:59:39  来源:igfitidea点击:

Manually position JComponent inside JPanel

javaswingjcomponent

提问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.

  1. Set the container's layout manager to null by calling setLayout(null).
  2. Call the Component class's setboundsmethod for each of the container's children.
  3. Call the Component class's repaintmethod.

创建没有布局管理器的容器包括以下步骤。

  1. 通过调用将容器的布局管理器设置为 null setLayout(null)
  2. setbounds为容器的每个子项调用 Component 类的方法。
  3. 调用 Component 类的repaint方法。