【问题标题】:Unable to layout properly with GridBagLayout无法使用 GridBagLayout 正确布局
【发布时间】:2017-02-21 16:42:50
【问题描述】:

我在开始构建的 GUI 中遇到 GridBagLayout 和 GridBagConstraints 问题。我必须图片,GUI 的当前状态之一,以及 GUI 的所需状态之一。我一直在尝试达到所需的状态,但一直无法:(。这是代码,我还将附上我上面提到的 2 张图片。此外,我格式化第一个或第二个复选框,但我一直无法弄清楚问题是什么。

驱动类:

import javax.swing.SwingUtilities;

public class Driver {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TheFrame();
            }

        });
    }
}

JFrame 类:

import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class TheFrame extends JFrame {

    //Declarations
    private GridBagConstraints gbc;
    private String myString;
    private JLabel selectionLab;
    private JCheckBox defconLevel1;
    private JCheckBox defconLevel2;
    private JCheckBox defconLevel3;
    private JCheckBox defconLevel4;
    private JCheckBox defconLevel5;

    public TheFrame() {
        super("DEFCON DEACTIVATOR");
        this.setSize(500,500);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.getContentPane().setLayout(new GridBagLayout());

        //Initialization
        gbc = new GridBagConstraints();
        selectionLab = new JLabel("Please Select DECON Level");
        defconLevel1 = new JCheckBox("DEFCON 1");
        defconLevel2 = new JCheckBox("DEFCON 2");
        defconLevel3 = new JCheckBox("DEFCON 3");
        defconLevel4 = new JCheckBox("DEFCON 4");
        defconLevel5 = new JCheckBox("DEFCON 5");

        //Configuration


        //Add to contentPane
        //ROW 1
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = gbc.NORTH;
        gbc.weighty = 1;
        gbc.insets = new Insets(0,0,0,0);
        this.getContentPane().add(selectionLab);

        //ROW 2
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = gbc.NORTH;
        gbc.weighty= 1;
        gbc.insets = new Insets(0,0,0,0);
        this.getContentPane().add(defconLevel1,gbc);
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.anchor = gbc.NORTH;
        gbc.weighty= 1;
        gbc.insets = new Insets(0,0,0,0);
        this.getContentPane().add(defconLevel2,gbc);
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.anchor = gbc.NORTH;
        gbc.weighty= 1;
        gbc.insets = new Insets(0,0,0,0);
        this.getContentPane().add(defconLevel3,gbc);
        gbc.gridx = 3;
        gbc.gridy = 1;
        gbc.anchor = gbc.NORTH;
        gbc.weighty= 1;
        gbc.insets = new Insets(0,0,0,0);
        this.getContentPane().add(defconLevel4,gbc);
        gbc.gridx = 4;
        gbc.gridy = 1;
        gbc.anchor = gbc.NORTH;
        gbc.weighty= 1;
        gbc.insets = new Insets(0,0,0,0);
        this.getContentPane().add(defconLevel5,gbc);
    }
}

更新代码:

驱动类

import javax.swing.SwingUtilities;

public class Driver {

    //Declarations
    private static SelectionPanel selectionPanel;
    private static HeaderPanel headerPanel;
    private static TheFrame frame = new TheFrame(selectionPanel,headerPanel);


//  public Driver() {
//      
//  }

    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Driver();
            }
        });
    }
}

框架类

import javax.swing.JFrame;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;

public class TheFrame extends JFrame {

    //Declarations
    private GridBagConstraints gbc;
    private SelectionPanel selectionPanel;
    private HeaderPanel headerPanel;

    public TheFrame(SelectionPanel selectionPanel, HeaderPanel headerPanel) {
        super("DEFCON DEACTIVATOR");
        this.selectionPanel = selectionPanel;
        this.headerPanel = headerPanel;

        //Initialization
        gbc = new GridBagConstraints();
        selectionPanel = new SelectionPanel();
        headerPanel = new HeaderPanel();
        this.getContentPane().setLayout(new GridBagLayout());

        //Configuration


        //Add to contentPane
        gbc.anchor = gbc.NORTH; //Content-Pane GLOBAL
        gbc.insets = new Insets(0,0,0,0); //Content-Pane GLOBAL
        gbc.weightx = 0; //Content-Pane GLOBAL

        gbc.weighty = 0.05;
        gbc.gridx = 0;
        gbc.gridy = 0;
        this.getContentPane().add(headerPanel,gbc);
        gbc.weighty = 1;
        gbc.gridx = 0;
        gbc.gridy = 1;
        this.getContentPane().add(selectionPanel,gbc);

        //Finalize JFrame Last Steps Configurations
        this.setSize(500,500);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

SelectionPanel 类

import java.awt.Insets;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

public class SelectionPanel extends JPanel {

    //Declarations
    private JCheckBox defconLevel1;
    private JCheckBox defconLevel2;
    private JCheckBox defconLevel3;
    private JCheckBox defconLevel4;
    private JCheckBox defconLevel5;
    private GridBagConstraints gbc;

    public SelectionPanel() {

        //Initializations
        defconLevel1 = new JCheckBox("DEFCON 1");
        defconLevel2 = new JCheckBox("DEFCON 2");
        defconLevel3 = new JCheckBox("DEFCON 3");
        defconLevel4 = new JCheckBox("DEFCON 4");
        defconLevel5 = new JCheckBox("DEFCON 5");
        gbc = new GridBagConstraints();

        //Configuration
        this.setLayout(new GridBagLayout());

        //Add
        //ROW 1
        gbc.insets = new Insets(0,0,0,0); //Content-Pane Global
        //gbc.anchor = gbc.EAST; //Makes Elements chain-follow each other
        gbc.gridy = 0;

        gbc.gridx = 0;
        this.add(defconLevel1,gbc);
        gbc.gridx = 1;
        this.add(defconLevel2,gbc);
        gbc.gridx = 2;
        this.add(defconLevel3,gbc);
        gbc.gridx = 3;
        this.add(defconLevel4,gbc);
        gbc.gridx = 4;
        this.add(defconLevel5,gbc);
    }
}

HeaderPanel 类

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

public class HeaderPanel extends JPanel {
    private JLabel headerLab;
    private GridBagConstraints gbc;

    public HeaderPanel() {
        //Initialize
        headerLab = new JLabel("PLEASE SELECT DEFCON LEVEL");
        gbc = new GridBagConstraints();

        //Configure
        this.setLayout(new GridBagLayout());

        //Add
        gbc.gridx = 0;
        gbc.gridy = 0;
        this.add(headerLab,gbc);
    }

}

现在的图片:

希望的设计:

更新图片:

【问题讨论】:

  • 你想让所有的复选框在窗口中水平居中吗?
  • 嗨 VGR,我遇到的问题是将“DEFCON 1”复选框与其余复选框均匀合并。如果仔细观察,您会发现“DEFCON 1”复选框相对于其他复选框具有额外的左侧间距。这是我面临的问题。感谢您顺便回复:)

标签: java swing user-interface layout-manager gridbaglayout


【解决方案1】:

标签的约束也需要:

    gbc.gridwitdh = 5;

这将允许标签占用与 5 个复选框相同的水平空间,从而允许每个复选框显示在其自己的列中。

在添加其他组件之前,您需要reset the gridwidth to 1

另一种选择可能是在您的面板上使用Titled Border。然后您可以使用FlowLayout 来添加所有复选框。这是一个更简单的解决方案,因为您无需担心所有 GridBagConstraints。

编辑:

阅读 How to Use GridBagLayout 上的 Swing 教程部分,了解有关所有约束的信息。

您需要做的第一件事是实际使用标签的约束,否则设置网格宽度不会做任何事情,因为将使用默认约束:

//this.getContentPane().add(selectionLab);
add(selectionLab, gbc);

它仍然看起来不正确,因为您需要了解与以下约束一起使用的正确值:

  1. 重量级
  2. 锚点
  3. 重量x

我会让你一次玩一个约束,看看当你改变约束时会发生什么。教程链接将对不同的值有所帮助。

【讨论】:

  • 嗯,我试过了,但似乎并没有改变这种情况。我遇到的问题是“DEFCON 1”和“DEFCON 2”jcheckboxes 之间创建的额外空间。额外的间距位于“DEFCON 1”jcheckbox 的右侧,如我分享的“Present Picture”图像所示。感谢您的回答,感谢您的帮助:)
  • 谢谢!我相信我的问题也来自我根据我提供的 JFrame 布局在 JFRame 中设置 JPanel 的方式。我决定创建一个包含我的标题的独特 JPanel。我将在问题中添加代码并发布我的新结果的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-12
  • 2020-01-10
  • 2011-07-12
  • 1970-01-01
  • 1970-01-01
  • 2020-09-18
  • 1970-01-01
相关资源
最近更新 更多