java - JPanel 不需要的位置和/或大小

标签 java swing size border jpanel

我正在创建一个程序,其中包含三个 JPanel - 2 个位于屏幕的上半部分,1 个位于屏幕的下半部分。这是像这样定位它们的代码:
people.setBounds(0, 0, Math.round(getWidth()/3), Math.round(getHeight()/2));
resources.setBounds(people.getX() + people.getWidth(), 0, Math.round(getWidth()/3), Math.round(getHeight()/2));
neighboring.setBounds(resources.getX() + resources.getWidth(), 0, Math.round(getWidth()/3), Math.round(getHeight()/2));
但不幸的是,这是我得到的结果:enter image description here
我试图让面板完美贴合 window ,但如您所见,它们比预期的要大一些。请帮忙?

最佳答案

使用布局管理器,而不是 setBounds,让布局为您完成工作。通过嵌套一些使用简单 GridLayout 的 JPanel,可以用最少的代码创建您的 GUI。有关更多信息,请查看 Lesson: Laying Out Components Within a Container

关于java - JPanel 不需要的位置和/或大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10060252/

相关文章:

java - 背景色调 TextView 不适用于 Lollipop 之前的设备

java - 我应该使用什么类来扩展 SwingWorker?

java - 从 GridBagLayout 中的特定坐标获取组件

java - 为什么它说 next(int) 在 java.util.Random 中具有 protected 访问权限?

c# - Visual Studio 2010 WPF 设计器规模

java - 在java中使用逻辑或而不重复条件中的变量

java - Android首次使用addValueEventListener的方法

java - 查找上午 8 点的时区名称

从不同目录编译相同的 c 程序会生成不同大小的目标代码

jquery - 如何获取 jQuery 对象的大小