import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class GridBagExample extends JPanel { private static final int WINDOW_WIDTH = 640; private static final int WINDOW_HEIGHT = 480; public static void main(final String[] args) { JFrame frame = new JFrame("Menu Example"); GridBagExample panel = new GridBagExample(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); frame.setLocationRelativeTo(null); frame.setContentPane(panel); panel.initWidgets(); frame.setVisible(true); } public void initWidgets() { setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); JButton button1 = new JButton("First Button"); c.gridx = 0; c.gridy = 0; //c.fill = GridBagConstraints.BOTH; //c.weightx = .5; add(button1, c); JButton button2 = new JButton("Second Button"); c.gridx = 1; c.gridy = 0; //c.weightx = .9; //c.weighty = .5; c.insets = new Insets(20, 10, 30, 5); add(button2, c); JButton button3 = new JButton("This button takes two cells"); c.gridx = 0; c.gridy = 1; c.gridwidth = 2; //c.weighty = .1; c.insets = new Insets(0, 0, 0, 0); c.ipady = 50; c.ipadx = 10; add(button3, c); JButton button4 = new JButton("Button on the side"); c.gridx = 2; c.gridy = 0; c.gridheight = 2; c.anchor = GridBagConstraints.SOUTH; c.weightx = .5; add(button4, c); } }