提问者:小点点

GridBagLayout不遵守gridx和gridy


在我正在制作的游戏中,我很难让一些组件正确布局。我的GUI由一个带有边框布局的外部JFrame组成,在边框布局的东侧,我有一个带有GridBagLayout的JPanel。我试图在JPanel中定位4个按钮(最终还有一些其他东西——但是当它不起作用时,我把它切到了按钮上),它们的网格和网格值设置为2行2列。但是,由于某种原因,它不起作用。它没有把按钮放在两行和列中,而是只显示一个按钮(屏幕截图)。

以下是我的代码:

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中使用它时,它失败了。


共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个对象错了gridbag都是敏感的!):

    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);