Uses a 2-column grid. : GridLayout « Swing « Java Tutorial






Uses a 2-column grid.
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.GridLayout;

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

public class GridLayoutDemo {
  public final static boolean RIGHT_TO_LEFT = false;

  public static void addComponentsToPane(Container pane) {
    if (RIGHT_TO_LEFT) {
      pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    }

    pane.setLayout(new GridLayout(0, 2));

    pane.add(new JButton("Button 1"));
    pane.add(new JButton("Button 2"));
    pane.add(new JButton("Button 3"));
    pane.add(new JButton("Long-Named Button 4"));
    pane.add(new JButton("5"));
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame("GridLayoutDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addComponentsToPane(frame.getContentPane());

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








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