微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java gridbaglayout 奇怪的行为

如何解决java gridbaglayout 奇怪的行为

我编写了这个简单的类来测试 gridbaglayout。 我的范围是将一些按钮对角线放入面板中,使用 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();
    }
}

解决方法

我的范围是将一些按钮放在面板的对角线上,使用 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)

但问题是当您指定 gridwidth 为 2 时,GridBagLayout 不知道第 1 列的宽度应该是多少,因此它使用 0。

实际上第 2 列变为第 1 列,按钮 3 绘制在第 1 列中。

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

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

,

我创建了以下不寻常的 GUI

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。