Java AWT GridBagLayout class

Introduction

GridBagLayout defines only one constructor, which is shown here:

GridBagLayout() 

import javax.swing.JFrame;
import java.awt.Container;
import javax.swing.JButton;
import java.awt.GridBagLayout;

public class Main {
    public static void main(String[] args) {
        String title = "GridBagLayout in its Simplest Form";
        JFrame frame = new JFrame(title);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridBagLayout());

        for (int i = 1; i <= 9; i++) {
            contentPane.add(new JButton("Button " + i));
        }/*from  w w  w.  java 2  s.  co m*/
        
        frame.pack();
        frame.setVisible(true);
    }
}
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;

class Demo extends JPanel implements ItemListener {

  String msg = "";
  JCheckBox windows, android, solaris, mac;

  public Demo() {
    GridBagLayout gbag = new GridBagLayout();
    GridBagConstraints gbc = new GridBagConstraints();
    setLayout(gbag);//from   w ww  .ja v a2 s . co m

    // Define check boxes.
    windows = new JCheckBox("Windows ", null, true);
    android = new JCheckBox("Android");
    solaris = new JCheckBox("Solaris");
    mac = new JCheckBox("Mac OS");

    gbc.weightx = 1.0;
    gbc.ipadx = 200; 
    gbc.insets = new Insets(4, 4, 0, 0); 

    gbc.anchor = GridBagConstraints.NORTHEAST;

    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbag.setConstraints(windows, gbc);

    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbag.setConstraints(android, gbc);

    // Give second row a weight of 1. I
    gbc.weighty = 1.0;
    gbc.gridwidth = GridBagConstraints.RELATIVE;
    gbag.setConstraints(solaris, gbc);

    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbag.setConstraints(mac, gbc);

    // Add the components.
    add(windows);
    add(android);
    add(solaris);
    add(mac);

    // Register to receive item events.
    windows.addItemListener(this);
    android.addItemListener(this);
    solaris.addItemListener(this);
    mac.addItemListener(this);
  }

  // Repaint when status of a check box changes.
  public void itemStateChanged(ItemEvent ie) {
    repaint();
  }

  // Display current state of the check boxes.
  public void paint(Graphics g) {
    msg = "Current state: ";
    g.drawString(msg, 6, 80);
    msg = "  Windows: " + windows.isSelected();
    g.drawString(msg, 6, 100);
    msg = "  Android: " + android.isSelected();
    g.drawString(msg, 6, 120);
    msg = "  Solaris: " + solaris.isSelected();
    g.drawString(msg, 6, 140);
    msg = "  Mac: " + mac.isSelected();
    g.drawString(msg, 6, 160);
  }
}

public class Main {
  public static void main(String[] args) {
    Demo panel = new Demo();

    JFrame application = new JFrame();

    application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    application.add(panel);
    application.setSize(250, 250);
    application.setVisible(true);
  }
}



PreviousNext

Related