我无法在正在制作的游戏中正确布置一些组件。我的GUI由一个带有BorderLayout的外部JFrame组成,而在BorderLayout的东侧,我有一个带有GridBagLayout的JPanel。我试图在JPanel中设置4个按钮(最后是其他一些东西 - 但我将它切换到按钮,当它不工作时),其gridx和gridy值设置为2行2列。但是,由于某种原因,它不起作用。它不是将按钮放在两行和列中,而是只显示一个按钮(screenshot)。
这是我的代码:
private class SidePanel extends JPanel
{
private GridLayout sidePaneLayout;
private JButton startButton;
private JButton quitButton;
private JButton saveButton;
private JButton loadButton;
private ButtonHandler buttonHandler;
private JLabel stardate;
private JLabel condition;
private JLabel position;
private JLabel warpFactor;
private JLabel energy;
private JLabel torpedos;
private JLabel shields;
private JLabel klingonsLeft;
private JPanel statusPanel;
public SidePanel()
{
super();
this.setLayout(new GridBagLayout());
GridBagConstraints constraints = new GridBagConstraints();
startButton = new JButton("New Game");
constraints.gridx = 0;
constraints.gridy = 0;
this.add(startButton, constraints);
quitButton = new JButton("Quit Game");
constraints.gridx = 0;
constraints.gridy = 1;
this.add(startButton, constraints);
saveButton = new JButton("Save Game");
constraints.gridx = 1;
constraints.gridy = 0;
this.add(startButton, constraints);
loadButton = new JButton("Load Game");
constraints.gridx = 1;
constraints.gridy = 1;
this.add(startButton, constraints);
/*Dimension buttonSize = new Dimension(155, 60);
startButton.setPreferredSize(buttonSize);
quitButton.setPreferredSize(buttonSize);
saveButton.setPreferredSize(buttonSize);
loadButton.setPreferredSize(buttonSize);*/
stardate = new JLabel("Stardate: ");
condition = new JLabel("Condition: ");
position = new JLabel("Position: ");
warpFactor = new JLabel ("Warp Factor: ");
energy = new JLabel("Energy: ");
torpedos = new JLabel("Torpedos: ");
shields = new JLabel("Shields: ");
klingonsLeft = new JLabel("Klingons Left: ");
statusPanel = new JPanel(new GridLayout(0, 2));
statusPanel.add(stardate);
statusPanel.add(condition);
statusPanel.add(position);
statusPanel.add(warpFactor);
statusPanel.add(energy);
statusPanel.add(torpedos);
statusPanel.add(shields);
statusPanel.add(klingonsLeft);
constraints.gridx = 0;
constraints.gridy = 2;
//this.add(statusPanel, constraints);
}
知道为什么这不起作用?我已经在独立的JFrame中使用GridBagLayout完成了一些其他测试,它们都可以正常工作。但是当我尝试在这个程序的内部JPanel中使用它时,它失败了。
答案 0 :(得分:2)
您正在添加startButton
按钮4次,这就是为什么它只显示一次。
this.add(startButton, constraints);
现在经过一些修正后,它看起来如下所示。
constraints.insets=new Insets(5, 5, 5, 5);// add top, left, bottom, right insets
...
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth=2; // span two column
this.add(statusPanel, constraints);
答案 1 :(得分:0)
将代码更改为此(即使1个对象错误,网格包也很敏感!):
startButton = new JButton("New Game");
constraints.gridx = 0;
constraints.gridy = 0;
this.add(startButton, constraints);
quitButton = new JButton("Quit Game");
constraints.gridx = 0;
constraints.gridy = 1;
this.add(quitButton , constraints);
saveButton = new JButton("Save Game");
constraints.gridx = 1;
constraints.gridy = 0;
this.add(saveButton , constraints);
loadButton = new JButton("Load Game");
constraints.gridx = 1;
constraints.gridy = 1;
this.add(loadButton , constraints);