GridLayout in Java Swing

GridLayout is a layout manager in Java Swing that arranges components in a grid of rows and columns. Each cell in the grid can contain one component, and all cells in a row or column have the same width or height.

Following is an example of how to use GridLayout in Java Swing:

import java.awt.GridLayout;

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

public class GridLayoutDemo {
	public static void main(String[] args) {
		JFrame frame = new JFrame("GridLayout Demo");
		frame.setLayout(new GridLayout(3, 3)); // set the layout manager

		// add buttons to the frame
		for (int i = 1; i <= 9; i++) {
			frame.add(new JButton(String.valueOf(i)));

		frame.setSize(300, 300);