无法使Java GridBagConstraints工作

时间:2012-08-24 23:43:15

标签: java graphics awt layout-manager gridbaglayout

一段时间以来一直在努力。我的方法如下:

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 doc之后。 - 这应该给我4个十字形的按钮。然而情况并非如此,并且按钮在窗口的中心堆叠在彼此之上。 我错过了什么?

3 个答案:

答案 0 :(得分:4)

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

为了我的钱,我会简化并使用简单的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);
   }
}

enter image description here

答案 1 :(得分:2)

你的意思是:

Cross my buttons

我摆脱了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();

    }
}

答案 2 :(得分:0)

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

来自Java Doc:

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行,一切都应该没问题。