// Purpose. GridBagLayout demo [source: Flanagan97b, p116] import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LayoutGridBagDemo { public static void main( String[] args ) { JFrame frame = new JFrame( "JButton demo" ); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); frame.getContentPane().setLayout( new GridBagLayout() ); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; // components grow in both dimensions c.insets = new Insets(5,5,5,5); // 5-pixel margins on all sides c.gridx = 0; c.gridy = 0; c.gridwidth = 4; c.gridheight = 4; c.weightx = 1.0; c.weighty = 1.0; // this button gets all extra space frame.getContentPane().add( new Button("Button #1"), c ); c.gridx = 4; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.weightx = 0.0; c.weighty = 0.0; frame.getContentPane().add( new Button("Button #2"), c ); c.gridx = 4; c.gridy = 1; c.gridwidth = 1; c.gridheight = 1; frame.getContentPane().add( new Button("Button #3"), c ); c.gridx = 4; c.gridy = 2; c.gridwidth = 1; c.gridheight = 2; frame.getContentPane().add( new Button("Button #4"), c ); c.gridx = 0; c.gridy = 4; c.gridwidth = 1; c.gridheight = 1; frame.getContentPane().add( new Button("Button #5"), c ); c.gridx = 2; c.gridy = 4; c.gridwidth = 1; c.gridheight = 1; frame.getContentPane().add( new Button("Button #6"), c ); c.gridx = 3; c.gridy = 4; c.gridwidth = 2; c.gridheight = 1; frame.getContentPane().add( new Button("Button #7"), c ); c.gridx = 1; c.gridy = 5; c.gridwidth = 1; c.gridheight = 1; frame.getContentPane().add( new Button("Button #8"), c ); c.gridx = 3; c.gridy = 5; c.gridwidth = 1; c.gridheight = 1; frame.getContentPane().add( new Button("Button #9"), c ); frame.pack(); frame.setVisible( true ); } }