Sample GridLayout Application : GridLayout « Swing « Java Tutorial






Sample GridLayout Application
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GridSizeTest extends JFrame {

  public static void main(String[] args) {
    GridSizeTest gst = new GridSizeTest();
    gst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gst.pack();
    gst.setVisible(true);
  }

  public GridSizeTest() {
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(2, 2));
    JButton button = new JButton("First");
    pane.add(button);
    button = new JButton("Second with a very long name");
    pane.add(button);
    button = new JButton("Hi");
    button.setFont(new Font("Courier", Font.PLAIN, 36));
    pane.add(button);
    button = new JButton("There");
    pane.add(button);
  }

}








14.90.GridLayout
14.90.1.Laying Out Components in a Grid
14.90.2.How to Use GridLayoutHow to Use GridLayout
14.90.3.Sample GridLayout ApplicationSample GridLayout Application
14.90.4.Creating Rows of ButtonsCreating Rows of Buttons
14.90.5.Uses a 2-column grid.Uses a 2-column grid.
14.90.6.Use a 1x1 grid to make a component as large as possibleUse a 1x1 grid to make a component as large as possible
14.90.7.Using a GridLayout ManagerUsing a GridLayout Manager