简体   繁体   中英

Unable to layout properly with GridBagLayout

I am having issues with GridBagLayout & GridBagConstraints in a GUI I am beginning to build. I have to pictures, one of the current state of the GUI, & one of the desired state of the GUI. I have been trying to reach the desired state but have been unable to :(. Here is the code, & I will also attach the 2 pictures I mentioned above. Moreover, there is an issue with the way that I am formatting the first or second checkbox, but I have been unable to figure out what the issue is.

Driver Class:

import javax.swing.SwingUtilities;

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

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

        });
    }
}

JFrame Class:

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

Updated Code:

Driver Class

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

TheFrame Class

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 Class

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 Class

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

}

Present Picture:

在此处输入图片说明

Wished Design:

在此处输入图片说明

Updated Image:

在此处输入图片说明

The constraint for the label also needs:

    gbc.gridwitdh = 5;

This will allow the label to take up the same horizontal space as the 5 checkboxes, allowing each check box to be displayed in its own column.

You will then need to reset the gridwidth to 1 before adding the other components.

Another option might be to use a Titled Border on your panel. Then you can just use a FlowLayout for adding all the check boxes. This is an easier solution since you don't need to worry about all the GridBagConstraints.

Edit:

Read the section from the Swing tutorial on How to Use GridBagLayout for information about all the constraints.

The first thing you need to do is actually use the constraints for the label otherwise setting the gridwidth won't do anything as the default constrainsts will be used:

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

It still won't look correct because you will then need to understand the proper values to be used with the following constraints:

  1. weighty
  2. anchor
  3. weightx

I'll let you play with the constraints one at a time to see what happens as you change the constraint. The tutorial link will help with the different values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM