To create a GridLayout of three rows with a horizontal gap of 10 pixels and a vertical gap of 20 pixels between cells
GridLayout gridLayout = new GridLayout(3, 0, 10, 20);
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 Main { public static void main(String[] args) { JFrame frame = new JFrame("GridLayout Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(2,2,20,20)); for (int i = 1; i <= 4; i++) { buttonPanel.add(new JButton("Button " + i)); }//from w w w. j av a 2 s . c om contentPane.add(buttonPanel, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } }