Laying Out Components in a Grid : GridLayout « Swing « Java Tutorial






import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JPanel;

public class Main {
  public static void main(String[] argv) throws Exception {

    JButton component1 = new JButton();
    JButton component2 = new JButton();
    
    int rows = 2;
    int cols = 2;
    JPanel panel = new JPanel(new GridLayout(rows, cols));
    panel.add(component1);
    panel.add(component2);

  }
}








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