Java/Swing JFC/GridBagLayout

Материал из Java эксперт
Перейти к: навигация, поиск

A GridBagLayout Example: weightx, weighty

 
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JPanel;
public class Main extends JPanel {
  protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) {
    Button button = new Button(name);
    gridbag.setConstraints(button, c);
    add(button);
  }
  public Main() {
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(gridbag);
    c.weightx = 1.0;
    c.weighty = 1.0;
    makebutton("Button 1", gridbag, c);
    c.fill = GridBagConstraints.BOTH;
    makebutton("Button 2", gridbag, c);
  }
  public static void main(String args[]) {
    Frame f = new Frame();
    JPanel mgb = new Main();
    f.add("Center", mgb);
    f.pack();
    f.setSize(300, 300);
    f.setVisible(true);
  }
}





Associate the gridbag constraints with the component

  
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();
    GridBagLayout gbl = new GridBagLayout();
    container.setLayout(gbl);
    
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;
    
    JButton component = new JButton("a");
    
    
    gbl.setConstraints(component, gbc);
    container.add(component);
    frame.pack();
    frame.setVisible(true);
  }
}





Create and set a gridbag layout and how to set gridbag constraints

  
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();
    GridBagLayout gbl = new GridBagLayout();
    container.setLayout(gbl);
    // Place a component at cell location (1,1)
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;
    
    JButton component = new JButton("a");
    
    // Associate the gridbag constraints with the component
    gbl.setConstraints(component, gbc);
    container.add(component);
    frame.pack();
    frame.setVisible(true);
  }
}





Getting the Number of Rows and Columns of Cells in a GridBagLayout

 
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();
    GridBagLayout gbl = new GridBagLayout();
    container.setLayout(gbl);
    GridBagConstraints gbc = new GridBagConstraints();
    JButton component1 = new JButton("a");
    JButton component2 = new JButton("b");
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbl.setConstraints(component1, gbc);
    container.add(component1);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbl.setConstraints(component2, gbc);
    container.add(component2);
    container.add(component1);
    container.add(component2);
    frame.pack();
    frame.setVisible(true);
    gbl.layoutContainer(container);
    int[][] dim = gbl.getLayoutDimensions();
    int cols = dim[0].length;
    int rows = dim[1].length;
    System.out.println(cols);
    System.out.println(rows);
  }
}





GridBagConstraints.BOTH

  
import java.awt.ruponent;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBag4 extends JPanel {
  GridBagConstraints constraints = new GridBagConstraints();
  public GridBag4() {
    setLayout(new GridBagLayout());
    constraints.fill = GridBagConstraints.BOTH;
    constraints.weighty = 1.0;
    int x, y; // for clarity
    constraints.weightx = 0.1;
    addGB(new JButton("one"), x = 0, y = 0);
    constraints.weightx = 0.5;
    addGB(new JButton("two"), ++x, y);
    constraints.weightx = 1.0;
    addGB(new JButton("three"), ++x, y);
  }
  void addGB(Component component, int x, int y) {
    constraints.gridx = x;
    constraints.gridy = y;
    add(component, constraints);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("GridBag4");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 100);
    frame.setLocation(200, 200);
    frame.setContentPane(new GridBag4());
    frame.setVisible(true);
  }
}





GridBagLayout Pane

  
/*
 * Copyright (c) 2000 David Flanagan. All rights reserved. This code is from the
 * book Java Examples in a Nutshell, 2nd Edition. It is provided AS-IS, WITHOUT
 * ANY WARRANTY either expressed or implied. You may study, use, and modify it
 * for any non-commercial purpose. You may distribute it non-commercially as
 * long as you retain this notice. For a commercial use license, or to purchase
 * the book (recommended), visit http://www.davidflanagan.ru/javaexamples2.
 */
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBagLayoutPane extends JPanel {
  public GridBagLayoutPane() {
    // Create and specify a layout manager
    this.setLayout(new GridBagLayout());
    // Create a constraints object, and specify some default values
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH; // components grow in both dimensions
    c.insets = new Insets(5, 5, 5, 5); // 5-pixel margins on all sides
    // Create and add a bunch of buttons, specifying different grid
    // position, and size for each.
    // Give the first button a resize weight of 1.0 and all others
    // a weight of 0.0. The first button will get all extra space.
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 4;
    c.gridheight = 4;
    c.weightx = c.weighty = 1.0;
    this.add(new JButton("Button #1"), c);
    c.gridx = 4;
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = c.weighty = 0.0;
    this.add(new JButton("Button #2"), c);
    c.gridx = 4;
    c.gridy = 1;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(new JButton("Button #3"), c);
    c.gridx = 4;
    c.gridy = 2;
    c.gridwidth = 1;
    c.gridheight = 2;
    this.add(new JButton("Button #4"), c);
    c.gridx = 0;
    c.gridy = 4;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(new JButton("Button #5"), c);
    c.gridx = 2;
    c.gridy = 4;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(new JButton("Button #6"), c);
    c.gridx = 3;
    c.gridy = 4;
    c.gridwidth = 2;
    c.gridheight = 1;
    this.add(new JButton("Button #7"), c);
    c.gridx = 1;
    c.gridy = 5;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(new JButton("Button #8"), c);
    c.gridx = 3;
    c.gridy = 5;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(new JButton("Button #9"), c);
  }
  public static void main(String[] a) {
    JFrame f = new JFrame();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    f.setContentPane(new GridBagLayoutPane());
    f.pack();
    f.setVisible(true);
  }
}





GridBagLayout with anchor constraints

  
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBagWithAnchor {
  public static void main(String[] args) {
    JFrame f = new JFrame(
        "Demonstrates the use of anchor constraints");
    JPanel p = new JPanel(new GridBagLayout());
    p.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2;
    c.anchor = GridBagConstraints.NORTH; // place component on the North
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.anchor = GridBagConstraints.SOUTHWEST;
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1;
    c.anchor = GridBagConstraints.CENTER; // remember to rest to center
    p.add(new JButton("and"), c);
    c.gridx = 2;
    p.add(new JButton("Support !!!"), c);
    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);
    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
  }
}





GridBagLayout with constraints

  
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBagWithContaints {
  public static void main(String[] args) {
    JFrame f = new JFrame(
        "Demonstrates the use of gridx, gridy,ipadx, ipady and insets constraints");
    JPanel p = new JPanel();
    p.setLayout(new GridBagLayout());
    // creates a constraints object
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2); // insets for all components
    c.gridx = 0; // column 0
    c.gridy = 0; // row 0
    c.ipadx = 5; // increases components width by 10 pixels
    c.ipady = 5; // increases components height by 10 pixels
    p.add(new JButton("Java"), c); // constraints passed in
    c.gridx = 1; // column 1
    // c.gridy = 0; // comment out this for reusing the obj
    c.ipadx = 0; // resets the pad to 0
    c.ipady = 0;
    p.add(new JButton("Source"), c);
    c.gridx = 0; // column 0
    c.gridy = 1; // row 1
    p.add(new JButton("and"), c);
    c.gridx = 1; // column 1
    p.add(new JButton("Support."), c);
    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);
    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
  }
}





GridBagLayout with gridwidth and gridheight constraints

  
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBagWithGridWidthHeight {
  public static void main(String[] args) {
    JFrame f = new JFrame(
        "Demonstrates the use of gridwidth, gridheight constraints");
    JPanel p = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2; // span across 2 rows
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1; // set back to 1 row
    c.gridwidth = 2; // span across 2 columns
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1; // set back to 1 column
    p.add(new JButton("and"), c);
    c.gridx = 2;
    p.add(new JButton("Support."), c);
    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);
    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
  }
}





GridBagLayout with weight constraint

  
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBagWithWeight {
  public static void main(String[] args) {
    JFrame f = new JFrame(
        "Demonstrates the use of fill constraints");
    JPanel p = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 2;
    c.fill = GridBagConstraints.BOTH; // Use both horizontal & vertical
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.fill = GridBagConstraints.HORIZONTAL; // Horizontal only
    p.add(new JButton("Source"), c);
    c.gridy = 1;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.NONE; // Remember to reset to none
    p.add(new JButton("and"), c);
    c.gridx = 2;
    c.fill = GridBagConstraints.VERTICAL; // Vertical only
    p.add(new JButton("Support."), c);
    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);
    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
  }
}





GridBagLayout with weightx and weighty constraints

  
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBagWithWeight {
  public static void main(String[] args) {
    JFrame f = new JFrame(
        "Demonstrates the use of weightx, weighty constraints");
    JPanel p = new JPanel();
    p.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    p.setLayout(new GridBagLayout());
    c = new GridBagConstraints();
    c.insets = new Insets(2, 2, 2, 2);
    c.weighty = 1.0;
    c.weightx = 1.0;
    c.gridx = 0;
    c.gridy = 0;
    p.add(new JButton("Java"), c);
    c.gridx = 1;
    p.add(new JButton("Source"), c);
    c.gridx = 0;
    c.gridy = 1;
    p.add(new JButton("and"), c);
    c.gridx = 1;
    p.add(new JButton("Support."), c);
    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);
    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
  }
}





Make the component only stretch horizontally

 

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();
    frame.setLayout(gbl);
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));
    gbl.layoutContainer(frame);
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbl.setConstraints(component, gbc);
    frame.add(component);
    frame.pack();
    frame.setVisible(true);
  }
}





Make the component only stretch vertically

 
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();
    frame.setLayout(gbl);
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));
    gbl.layoutContainer(frame);
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.VERTICAL;
    gbl.setConstraints(component, gbc);
    frame.add(component);
    frame.pack();
    frame.setVisible(true);
  }
}





Make the component on stretchable

 
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();
    frame.setLayout(gbl);
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));
    gbl.layoutContainer(frame);
    
    GridBagConstraints gbc = new GridBagConstraints();
    
    
    gbc.fill = GridBagConstraints.NONE;
    
    gbl.setConstraints(component, gbc);
    frame.add(component);
    frame.pack();
    frame.setVisible(true);
  }
}





Make the component stretch in both directions

 
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();
    frame.setLayout(gbl);
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));
    gbl.layoutContainer(frame);
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.BOTH;
    gbl.setConstraints(component, gbc);
    frame.add(component);
    frame.pack();
    frame.setVisible(true);
  }
}





Making a GridBagLayout Fill the Container

 
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();
    frame.setLayout(gbl);
    frame.add(new JButton("1"));
    frame.add(new JButton("2"));
    gbl.layoutContainer(frame);
    double[][] weights = gbl.getLayoutWeights();
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < weights[i].length; j++) {
        weights[i][j] = 1;
      }
    }
    gbl.columnWeights = weights[0];
    gbl.rowWeights = weights[1];
    frame.pack();
    frame.setVisible(true);
  }
}





Place a component at (0,1) with a column weight 1 and a row weight of 2

 
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();
    frame.setLayout(gbl);
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));
    gbl.layoutContainer(frame);
    
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.weightx = 1;
    gbc.weighty = 2;
    gbl.setConstraints(component, gbc);

    frame.pack();
    frame.setVisible(true);
  }
}





Place a component at cell location (1,1)

 
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();
    GridBagLayout gbl = new GridBagLayout();
    container.setLayout(gbl);
    
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;
    
    JButton component = new JButton("a");
    
    
    gbl.setConstraints(component, gbc);
    container.add(component);
    frame.pack();
    frame.setVisible(true);
  }
}





Positioning a component in the center of other component using GridbagLayout

 
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main {
  public static void main(String[] argv) {
    JFrame demo = new JFrame("GridBag demo, to center a component");
    JPanel parentPanel = new JPanel();
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    constraints.fill = GridBagConstraints.CENTER;
    gridbag.setConstraints(parentPanel, constraints);
    parentPanel.setLayout(gridbag);
    Label centerLabel = new Label(" AAA...");
    parentPanel.add(centerLabel);
    demo.add(parentPanel);
    demo.setSize(500, 500);
    demo.setVisible(true);
  }
}





Setting Stretchyness of a GridBagLayout Using Fill

 
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();
    frame.setLayout(gbl);
    JButton component = new JButton("1");
    frame.add(component);
    frame.add(new JButton("2"));
    gbl.layoutContainer(frame);
    
    GridBagConstraints gbc = new GridBagConstraints();
    
    // Make the component on stretchable
    gbc.fill = GridBagConstraints.NONE;
    
    gbl.setConstraints(component, gbc);
    frame.add(component);
    frame.pack();
    frame.setVisible(true);
  }
}





Setting the Location of a Component in a GridBagLayout

 
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();
    GridBagLayout gbl = new GridBagLayout();
    container.setLayout(gbl);
    GridBagConstraints gbc = new GridBagConstraints();
    JButton component1 = new JButton("a");
    JButton component2 = new JButton("b");
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbl.setConstraints(component1, gbc);
    container.add(component1);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbl.setConstraints(component2, gbc);
    container.add(component2);
    container.add(component1);
    container.add(component2);
    frame.pack();
    frame.setVisible(true);
  }
}





Setting the Space around a Component Within the Cell of the GridBagLayout Using Insets

 
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();
    frame.setLayout(gbl);
    JButton component = new JButton("1");
    frame.add(new JButton("2"));
    frame.add(new JButton("3"));
    frame.add(new JButton("4"));
    frame.add(new JButton("5"));
    frame.add(component);
    frame.add(new JButton("6"));
    frame.add(new JButton("7"));
    frame.add(new JButton("8"));
    frame.add(new JButton("9"));
    frame.add(new JButton("0"));
    
    gbl.layoutContainer(frame);
    GridBagConstraints gbc = new GridBagConstraints();
    int top = 20;
    int left = 20;
    int bottom = 2;
    int right = 40;
    gbc.insets = new Insets(top, left, bottom, right);
    gbl.setConstraints(component, gbc);

    frame.pack();
    frame.setVisible(true);
  }
}





Setting the Stretchyness of Rows and Columns in a GridBagLayout Using Layout Weights

 
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
  public static void main(String[] argv) throws Exception {
    JFrame frame = new JFrame();
    GridBagLayout gbl = new GridBagLayout();
    frame.setLayout(gbl);
    frame.add(new JButton("1"));
    frame.add(new JButton("2"));
    gbl.layoutContainer(frame);

    gbl.columnWeights = new double[]{0.0f, 1.0f, 2.0f};
    gbl.rowWeights = new double[]{0.0f, 1.0f};
    frame.pack();
    frame.setVisible(true);
  }
}





Simplest gridbag layout

  
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SimpleGridBag {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    JPanel p = new JPanel();
    p.setLayout(new GridBagLayout());
    p.add(new JButton("Java"));
    p.add(new JButton("Source"));
    p.add(new JButton("and"));
    p.add(new JButton("Support."));
    WindowListener wndCloser = new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    };
    f.addWindowListener(wndCloser);
    f.getContentPane().add(p);
    f.setSize(600, 200);
    f.show();
  }
}





Work GridBagConstraints 3

  
import java.awt.ruponent;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBag3 extends JPanel {
  GridBagConstraints constraints = new GridBagConstraints();
  public GridBag3() {
    setLayout(new GridBagLayout());
    constraints.weightx = 1.0;
    constraints.weighty = 1.0;
    constraints.fill = GridBagConstraints.BOTH;
    int x, y;
    constraints.gridheight = 2; // span two rows
    addGB(new JButton("one"), x = 0, y = 0);
    constraints.gridheight = 1; // set it back
    addGB(new JButton("two"), x = 1, y = 0);
    addGB(new JButton("three"), x = 2, y = 0);
    constraints.gridwidth = 2; // span two columns
    addGB(new JButton("four"), x = 1, y = 1);
    constraints.gridwidth = 1; // set it back
  }
  void addGB(Component component, int x, int y) {
    constraints.gridx = x;
    constraints.gridy = y;
    add(component, constraints);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("GridBag3");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setLocation(200, 200);
    frame.setContentPane(new GridBag3());
    frame.setVisible(true);
  }
}





Work with GridBagConstraints 1

  
import java.awt.ruponent;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBag1 extends JPanel {
  GridBagConstraints constraints = new GridBagConstraints();
  public GridBag1() {
    setLayout(new GridBagLayout());
    int x, y; // for clarity
    addGB(new JButton("North"), x = 1, y = 0);
    addGB(new JButton("West"), x = 0, y = 1);
    addGB(new JButton("Center"), x = 1, y = 1);
    addGB(new JButton("East"), x = 2, y = 1);
    addGB(new JButton("South"), x = 1, y = 2);
  }
  void addGB(Component component, int x, int y) {
    constraints.gridx = x;
    constraints.gridy = y;
    add(component, constraints);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("GridBag1");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(225, 150);
    frame.setLocation(200, 200);
    frame.setContentPane(new GridBag1());
    frame.setVisible(true);
  }
}





Work with GridBagConstraints 2

  
import java.awt.ruponent;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBag2 extends JPanel {
  GridBagConstraints constraints = new GridBagConstraints();
  public GridBag2() {
    setLayout(new GridBagLayout());
    constraints.weightx = 1.0;
    constraints.weighty = 1.0;
    constraints.fill = GridBagConstraints.BOTH;
    int x, y; // for clarity
    addGB(new JButton("North"), x = 1, y = 0);
    addGB(new JButton("West"), x = 0, y = 1);
    addGB(new JButton("Center"), x = 1, y = 1);
    addGB(new JButton("East"), x = 2, y = 1);
    addGB(new JButton("South"), x = 1, y = 2);
  }
  void addGB(Component component, int x, int y) {
    constraints.gridx = x;
    constraints.gridy = y;
    add(component, constraints);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("GridBag2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(225, 150);
    frame.setLocation(200, 200);
    frame.setContentPane(new GridBag2());
    frame.setVisible(true);
  }
}





Work with GridBagConstraints 5

  
import java.awt.ruponent;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GridBag5 extends JPanel {
  GridBagConstraints constraints = new GridBagConstraints();
  public GridBag5() {
    setLayout(new GridBagLayout());
    int x, y; // for clarity
    addGB(new JButton("North"), x = 1, y = 0);
    constraints.ipadx = 25; // add padding
    constraints.ipady = 25;
    addGB(new JButton("West"), x = 0, y = 1);
    constraints.ipadx = 0; // remove padding
    constraints.ipady = 0;
    addGB(new JButton("Center"), x = 1, y = 1);
    addGB(new JButton("East"), x = 2, y = 1);
    addGB(new JButton("South"), x = 1, y = 2);
  }
  void addGB(Component component, int x, int y) {
    constraints.gridx = x;
    constraints.gridy = y;
    add(component, constraints);
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("GridBag5");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(250, 250);
    frame.setLocation(200, 200);
    frame.setContentPane(new GridBag5());
    frame.setVisible(true);
  }
}