0
我正尝试使用GridBagLayout构建一个小型Java游戏的GUI。最后,它应该是这样的:使用GridBagLayout构建GUI(Java)
这是我的代码:
private GridBagConstraints c;
...
c.gridx = 1;
c.gridy = 0;
c.weightx = 0;
add(playButton, c);
c.gridx = 1;
c.gridy = 1;
c.weightx = 0;
add(optionsButton, c);
c.gridx = 1;
c.gridy = 2;
c.weightx = 0;
add(manualButton, c);
c.gridx = 1;
c.gridy = 3;
c.weightx = 0;
add(exitButton, c);
c.gridx = 0;
c.gridy = 4;
c.weightx = 1;
c.anchor = GridBagConstraints.SOUTHWEST;
add(creditsButton, c);
c.gridx = 2;
c.gridy = 4;
c.weightx = 1;
c.anchor = GridBagConstraints.SOUTHEAST;
add(legalNoticeButton, c);
目前,它看起来像这样:
我的问题是我是谁可以将两个按钮设置到底部而不将其他四个底部设置到顶部?
您可以尝试使用BorderLayout,其中位于中心位置的JPanel中的游戏按钮,位于南部位置的JPanel中的底部按钮以及在北部,东部和西部位置中没有内容的虚拟JPanel。 – 2015-04-02 16:31:19
使用“weighty”和“anchor”属性有没有更简单的解决方案? – 2015-04-02 16:53:25
另一种方法是创建一个3列乘8行矩阵,使用带有空白文本的虚拟JLabel填充不需要JButton的地方。 – 2015-04-02 16:57:03