我的使用 BorderLayout 的 JPanel 遇到了一个恼人的问题。
this.setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel(new BorderLayout(0, 0));
panel.add(new Slot(), BorderLayout.SOUTH);
panel.add(new MapGUI(), BorderLayout.NORTH);
this.add(panel, BorderLayout.CENTER);
这是在我的 Inventory JPanel 的构造函数中。 Slot() 和 MapGUI() 是扩展 JPanel 的类。游戏运行时,屏幕如下所示:
显然,面板完全关闭,两个面板都离开屏幕。我怎样才能解决这个问题?感谢您的帮助。
最佳答案
根据 BorderLayout 的 JavaDocs:
The components are laid out according to their preferred sizes and the constraints of the container's size. The NORTH and SOUTH components may be stretched horizontally; the EAST and WEST components may be stretched vertically; the CENTER component may stretch both horizontally and vertically to fill any space left over.
您设置了首选尺寸吗?
您可能想尝试BoxLayout如果你想垂直对齐组件。
关于java - 使用 BorderLayout 时 JPanel 的间距不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28033255/