我正在创建一个程序,其中包含三个 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));
但不幸的是,这是我得到的结果:
我试图让面板完美贴合 window ,但如您所见,它们比预期的要大一些。请帮忙?
最佳答案
使用布局管理器,而不是 setBounds,让布局为您完成工作。通过嵌套一些使用简单 GridLayout 的 JPanel,可以用最少的代码创建您的 GUI。有关更多信息,请查看 Lesson: Laying Out Components Within a Container
关于java - JPanel 不需要的位置和/或大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10060252/