使用 GridBagLayout 的意外结果

Undesired outcome using GridBagLayout

我一直在尝试通过 Youtube 视频学习,但我一定缺少有关 GridBagLayout 的某些内容。

基本上我期望的是 4 个按钮,一个在另一个下面,高度为 2 个网格,但似乎无论我设置什么值 gridheight,按钮都保持相同大小

可编译代码:

package test;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class Test{

 public static void main(String []args){
    System.out.println("Hello World");

    JFrame jay = new JFrame("test");
    JPanel jp = new JPanel(new GridBagLayout());
    GridBagConstraints gc = new GridBagConstraints();

    JButton j1 = new JButton("Button 1");
    JButton j2 = new JButton("Button 22222222222222");
    JButton j3 = new JButton("Button 333333");
    JButton j4 = new JButton("4");

    gc.gridx = 0; gc.gridy = 0;
    gc.insets = new Insets(5,10,5,10);
    gc.gridheight = 2; //This is where the problem is;
    gc.fill = GridBagConstraints.BOTH;
    jp.add(j1,gc);
    gc.gridx = 0; gc.gridy = 2;
    jp.add(j2,gc);
    gc.gridx = 0; gc.gridy = 4;
    jp.add(j3,gc);
    gc.gridx = 0; gc.gridy = 6;
    jp.add(j4,gc);
    jay.add(jp);
    jay.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jay.setSize(300,350);
    jay.setVisible(true);

 }
}

此外,如果有人向我解释是什么决定了网格的大小?只是组件大小吗?

gridheight 指定组件将扩展的行数,假设这些行中有组件(任何空行的大小将自动调整为 0

how would I go about achieving taller buttons then? adding blank components?

一种可能的解决方案是使用 ipady 添加到组件的 preferredSize 属性(您也可以使用 ipadx 作为宽度)

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gc = new GridBagConstraints();

            JButton j1 = new JButton("Button 1");
            JButton j2 = new JButton("Button 22222222222222");
            JButton j3 = new JButton("Button 333333");
            JButton j4 = new JButton("4");

            gc.insets = new Insets(5, 10, 5, 10);
            gc.ipady = 50;
            gc.gridwidth = GridBagConstraints.REMAINDER;
            gc.fill = GridBagConstraints.BOTH;
            add(j1, gc);
            add(j2, gc);
            add(j3, gc);
            add(j4, gc);
        }

    }
}

查看 How to Use GridBagLayout 了解更多详情