GridBagLayout 奇怪的行为

GridBagLayout strange behaviour

我写了这个简单的class来测试GridBagLayout

我的范围是使用网格包布局并设置 gridxgridy,将一些按钮沿对角线放置到面板中,但我有一个奇怪的行为。 如果我将 gridwidth = 2 放在按钮 "2" 上,按钮 "3" 将绘制在标记为 "2" 的按钮下。

我的 class 是一个简单的演示,但我无法弄清楚它有什么问题 猜猜为什么?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Test extends JFrame {

    private JPanel panel;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b4;
    private JButton b5;
    private JLabel label1;

    public Test() {

        panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setBackground(Color.DARK_GRAY);
        GridBagConstraints gbc = new GridBagConstraints();
        b1 = new JButton("1");
        gbc.gridx = 0;
        gbc.gridy = 0;
        panel.add(b1, gbc);

        b2 = new JButton("2");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;

        gbc.gridwidth = 2;

        panel.add(b2, gbc);

        gbc = new GridBagConstraints();
        b3 = new JButton("3");
        gbc.gridx = 2;
        gbc.gridy = 2;
        panel.add(b3, gbc);

        gbc = new GridBagConstraints();
        b4 = new JButton("4");
        gbc.gridx = 3;
        gbc.gridy = 3;
        panel.add(b4, gbc);

        gbc = new GridBagConstraints();
        b5 = new JButton("5");
        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(b5, gbc);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation(500, 400);
        this.setSize(800, 300);
        this.setTitle("Frame principale dell'applicazione");
        this.setResizable(true);

        getContentPane().add(panel, BorderLayout.CENTER);

        this.setVisible(true);
    }

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

My scope was to put some buttons in diagonal into panel, using gridbaglayout and setting gridx and gridy, but I have a strange behaviour. if I put gridwidth = 2 on button "2", button "3" will be drawn under button labeled "2" .

Button 1 is added to (0, 0) // ok
Button 3 is added to (2, 2) // doesn't work

按钮 3 未添加到第 2 列,因为第 1 列没有宽度。

是的,您尝试添加:

Button 2 to (1, 1)

但问题是当您将网格宽度指定为 2 时,GridBagLayout 不知道第 1 列的宽度应该是多少,所以它使用 0。

实际上第 2 列变成了第 1 列并且 Button 3 绘制在第 1 列中。

通常,列没有宽度,除非您向该列添加网格宽度 = 1 的组件。

如果你想随机添加组件到网格,那么你需要为 GridBagLayout 中的每一列配置一个最小宽度。有关此方法的示例,请参阅:

我创建了以下不寻常的 GUI

这是我所做的。

  1. 您只需要 GridBagConstraints.

    的一个实例
  2. 我将所有 gridwidth 值设为 1。

  3. 我把gbc.fill设为GridBagConstraints.NONE

这是我的代码运行。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GridBagLayoutTestGUI extends JFrame {

    private static final long serialVersionUID = 1L;
    
    private JPanel panel;
    private JButton b1;
    private JButton b2;
    private JButton b3;
    private JButton b4;
    private JButton b5;
    private JLabel label1;

    public GridBagLayoutTestGUI() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("Frame principale dell'applicazione");

        this.panel = createMainPanel();
        getContentPane().add(panel, BorderLayout.CENTER);

        this.pack();
        this.setLocationByPlatform(true);
        this.setVisible(true);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.setBackground(Color.DARK_GRAY);
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.NONE;
        b1 = new JButton("1");
        panel.add(b1, gbc);
        
        gbc.gridx = 1;
        gbc.gridy = 1;
        b2 = new JButton("2");
        panel.add(b2, gbc);

        gbc.gridx = 2;
        gbc.gridy = 2;
        b3 = new JButton("3");
        panel.add(b3, gbc);

        gbc.gridx = 3;
        gbc.gridy = 3;
        b4 = new JButton("4");
        panel.add(b4, gbc);

        gbc.gridx = 4;
        gbc.gridy = 4;
        b5 = new JButton("5");
        panel.add(b5, gbc);
        
        return panel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GridBagLayoutTestGUI();
            }
        });
    }
}