无法使java gridbagconstraints工作

bn31dyow  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(351)

我为此奋斗了一段时间。我的方法如下:

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文档之后-这应该给我四个十字形的纽扣。但事实并非如此,按钮在窗口的中心是一个一个叠在一起的。我错过了什么?

vfwfrxfs

vfwfrxfs1#

当你使用 gridHeight 以及 gridWidth 你说的是“这个组件应该占用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.

这里,“组件”是添加到面板的组件。你箱子里的纽扣。
照其他海报上说的做,去掉网格宽度和网格高度线,一切都应该很好。

xpcnnkqh

xpcnnkqh2#

为什么使用gridheight和gridwidth为3?至少说起来有点奇怪。
为了我的钱,我会简化事情并使用简单的网格布局:

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

ippsafx7

ippsafx73#

你的意思是:

我会把那些 gridwidth 以及 gridheight ```
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();

}

}

相关问题