gpt4 book ai didi

java - GridBagLayout 不遵守 gridx 和 gridy

转载 作者:行者123 更新时间:2023-12-02 05:51:04 25 4
gpt4 key购买 nike

我在制作的游戏中无法正确布置某些组件。我的 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 中使用它时,它失败了。

最佳答案

您添加了 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);

enter image description here

关于java - GridBagLayout 不遵守 gridx 和 gridy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23529245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com