【问题标题】:GridBagLayout strange behaviourGridBagLayout 奇怪的行为
【发布时间】:2021-06-08 20:46:23
【问题描述】:

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

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

我的课是一个简单的演示,但我不知道它有什么问题 猜猜为什么?

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

【问题讨论】:

  • 当您将 1 添加到 gridy 时,gridx 应该回到零。你希望你的 GUI 看起来像什么? JButton 的正确布局是什么?

标签: java swing awt layout-manager gridbaglayout


【解决方案1】:

我的范围是将一些对角线按钮放入面板中,使用 gridbaglayout 并设置 gridx 和 gridy,但我有一个奇怪的行为。如果我将 gridwidth = 2 放在按钮“2”上,按钮“3”将绘制在标记为“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 列,而按钮 3 被绘制在第 1 列中。

一般来说,一列没有宽度,除非您向该列添加一个 gridwidth = 1 的组件。

如果您想将组件随机添加到网格中,则需要为 GridBagLayout 中的每一列配置最小宽度。有关此方法的示例,请参阅:Creating a board game layout using JLayeredPane。

【讨论】:

  • @aleScaramuzza,您的问题是:如果我将 gridwidth = 2 放在按钮“2”上,按钮“3”将绘制在标记为“2”的按钮下 - 含义如果您删除 gridwidth = 2,那么它会按预期显示。这是否不能回答使用 1 以外的网格宽度如何影响布局的问题?不要忘记“接受”解释问题的答案。发布没有解释的代码不是答案。
【解决方案2】:

我创建了以下不寻常的 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();
            }
        });
    }
}

【讨论】:

    猜你喜欢
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 2010-10-03
    • 2021-07-12
    • 2013-10-04
    相关资源
    最近更新 更多