Java GridBagLayout不符合gridx和gridy
在我正在制作的游戏中,我很难正确地布置一些组件。我的GUI由一个带有BorderLayout的外部JFrame组成,在BorderLayout的东侧有一个带有GridBagLayout的JPanel。我试图在JPanel中定位4个按钮(最终还有一些其他的东西——但当它不工作时,我将其缩减为按钮),并将它们的gridx和gridy值设置为2行2列。然而,由于某些原因,它不起作用。它没有将按钮分为两行和两列,而是只显示一个按钮() 这是我的密码:Java GridBagLayout不符合gridx和gridy,java,swing,layout,gridbaglayout,Java,Swing,Layout,Gridbaglayout,在我正在制作的游戏中,我很难正确地布置一些组件。我的GUI由一个带有BorderLayout的外部JFrame组成,在BorderLayout的东侧有一个带有GridBagLayout的JPanel。我试图在JPanel中定位4个按钮(最终还有一些其他的东西——但当它不工作时,我将其缩减为按钮),并将它们的gridx和gridy值设置为2行2列。然而,由于某些原因,它不起作用。它没有将按钮分为两行和两列,而是只显示一个按钮() 这是我的密码: private class SidePanel ex
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);
}
你知道为什么这样不行吗?我在独立的JFrames中使用GridBagLayout做了一些其他测试,它们都可以工作。但是当我尝试在这个程序的内部JPanel中使用它时,它失败了 您添加了四次
开始按钮
按钮,这就是为什么它只显示一次
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);
将您的代码更改为此(即使有一个对象出错,gridbag也是敏感的!):
你会希望为我们创建并发布一个网站,以便能够提供最好的帮助。哇,谢谢你,Braj,很抱歉。我不知道自己做错了什么,我确信自己没有犯愚蠢的错误。我想我错了
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);