2

为此苦苦挣扎了一段时间。我的方法如下所示:

public Frame(){
            JFrame window = new JFrame();
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setSize(800, 600);

            JPanel panel = new JPanel(new GridBagLayout());

            GridBagConstraints gbc = new GridBagConstraints();
              gbc.gridheight =3;
              gbc.gridwidth = 3;


            JButton upButt = new JButton();//Buttons.upButton();
              gbc.gridx = 1;
              gbc.gridy = 0;
              panel.add(upButt, gbc);

            JButton downButt = new JButton();
              gbc.gridx = 1;
              gbc.gridy = 2;
              panel.add(downButt, gbc);

            JButton leftButt = new JButton();//Buttons.leftButton();
              gbc.gridx=0;
              gbc.gridy = 1;
              panel.add(leftButt, gbc);

            JButton rightButt = new JButton();//Buttons.rightButton();
              gbc.gridx =2;
              gbc.gridy = 1;
              panel.add(rightButt, gbc);

            window.add(panel);
            window.setVisible(true);

           }

据我了解 - 在阅读和重读 Java 文档之后。- 这应该给我 4 个十字形的按钮。然而,情况并非如此,按钮在窗口中心彼此堆叠。我错过了什么?

4

3 回答 3

4

为什么使用 3 的 gridheight 和 gridwidth?至少可以说这有点奇怪。

为了我的钱,我会简化事情并使用简单的 GridLayout:

import java.awt.GridLayout;
import javax.swing.*;

public class Foo003 {
   public static void main(String[] args) {
      JButton upButton = new JButton("Up");
      JButton downButton = new JButton("Down");
      JButton leftButton = new JButton("Left");
      JButton rightButton = new JButton("Right");
      JComponent[][] components = { 
            { new JLabel(), upButton, new JLabel() },
            { leftButton, new JLabel(), rightButton },
            { new JLabel(), downButton, new JLabel() } };

      JPanel panel = new JPanel(new GridLayout(components.length,
            components[0].length, 8, 8));
      for (int i = 0; i < components.length; i++) {
         for (int j = 0; j < components[i].length; j++) {
            panel.add(components[i][j]);
         }
      }

      int eb = 15;
      panel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));

      JFrame frame = new JFrame("Grid e.g.");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(panel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

在此处输入图像描述

于 2012-08-24T23:46:04.450 回答
2

你的意思是这样的:

越过我的按钮

我会摆脱gridwidthgridheight

public class TestGridBagLayout extends JFrame {

    public TestGridBagLayout() {

        setTitle("Test");
        setLayout(new GridBagLayout());
        setSize(200, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        add(createButton(), gbc);

        gbc.gridy = 2;
        add(createButton(), gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        add(createButton(), gbc);

        gbc.gridx = 2;
        add(createButton(), gbc);

        setVisible(true);

    }

    protected JButton createButton() {

        return new JButton("+");

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        new TestGridBagLayout();

    }
}
于 2012-08-24T23:54:26.950 回答
0

当您使用时gridHeightgridWidth您所说的是“此组件应占用 3 行和 3 列”。

来自 Java 文档:

gridheight
Specifies the number of cells in a column for the component's display area.

gridwidth
Specifies the number of cells in a row for the component's display area.

这里,“组件”是要添加到面板的组件。你的情况下的按钮。

正如其他海报所说的那样,删除 gridWidth 和 gridHeight 线,一切都应该没问题。

于 2012-08-25T00:26:46.530 回答