Java GridBagLayout Example
Chapter:
Swing
Last Updated:
27-09-2016 16:44:26 UTC
Program:
/* ............... START ............... */
import java.awt.Container;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JavaGridBagLayoutExample {
public static void main(String[] args) {
String title = "GridBagLayout";
JFrame frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridBagLayout());
for (int i = 1; i <= 5; i++) {
contentPane.add(new JButton("Button " + i));
}
frame.pack();
frame.setVisible(true);
}
}
/* ............... END ............... */
Output
Notes:
-
The GridBagLayout lays out components in a grid of cells arranged in rows and columns similar to the GridLayout.
Tags
GridBagLayout, Java, Swing