Java examples for Swing:GridBagLayout
insets constraint sets the external padding around the component.
It adds spaces around the component.
import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; public class Main { public static void main(String[] args) { JFrame frame = new JFrame("GridBagLayout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.fill = GridBagConstraints.BOTH; gbc.insets = new Insets(5, 5, 5, 5); int count = 1; for (int y = 0; y < 3; y++) { gbc.gridy = y;/* w ww . j a v a 2 s . c o m*/ for (int x = 0; x < 3; x++) { gbc.gridx = x; frame.add(new JButton("Button " + count++), gbc); } } frame.add(new JButton("Button 9"), gbc); frame.pack(); frame.setVisible(true); } }