Java GridLayout Example
Chapter:
Swing
Last Updated:
27-09-2016 16:35:43 UTC
Program:
/* ............... START ............... */
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JavaGridLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("GridLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 0));
for (int i = 1; i <= 9; i++) {
buttonPanel.add(new JButton("Button " + i));
}
contentPane.add(buttonPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
/* ............... END ............... */
Output
Notes:
-
A GridLayout arranges components in a grid with equally sized cells. Each component is placed in one cell.
Tags
GridLayout, Java, Swing