// Purpose. GridBagLayout and BoxLayout demo [source: Topley, p151] import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; public class LayoutBoxDemo extends JPanel { public static void addTitledBorder(JPanel p, String t, Border b) { p.setBorder( BorderFactory.createTitledBorder( b, t, TitledBorder.TOP, TitledBorder.CENTER ) ); } public static void main(String[] args) { JFrame f = new JFrame("Box Layout Demo"); f.getContentPane().setLayout( new GridBagLayout() ); Dimension horzRigidAreaDim = new Dimension(8,40); Dimension vertRigidAreaDim = new Dimension(40,64); int strutWidth = 16; int strutHeight = 32; Border b = BorderFactory.createEtchedBorder(); JPanel p; GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = 0; c.gridwidth = 1; c.gridheight = 1; c.weightx = 3.0; c.weighty = 1.0; c.fill = GridBagConstraints.BOTH; c.insets = new Insets(2, 2, 2, 2); addTitledBorder( p = new JPanel(), "1: Nothing", b ); p.setLayout( new BoxLayout( p, BoxLayout.X_AXIS ) ); p.add( new JButton("L") ); p.add( new JButton("M") ); p.add( new JButton("R") ); f.getContentPane().add( p, c ); addTitledBorder( p = new JPanel(), "2: Glue", b ); p.setLayout( new BoxLayout( p, BoxLayout.X_AXIS ) ); p.add( Box.createGlue() ); p.add( new JButton("L") ); p.add( Box.createGlue() ); p.add( new JButton("M") ); p.add( Box.createGlue() ); p.add( new JButton("R") ); p.add( Box.createGlue() ); c.gridy = 1; f.getContentPane().add( p, c ); addTitledBorder( p = new JPanel(), "3: Struts", b ); p.setLayout( new BoxLayout( p, BoxLayout.X_AXIS ) ); p.add( Box.createHorizontalStrut( strutWidth ) ); p.add(new JButton("L") ); p.add( Box.createHorizontalStrut( strutWidth ) ); p.add(new JButton("M") ); p.add(new JButton("R") ); c.gridy = 2; f.getContentPane().add( p, c ); addTitledBorder( p = new JPanel(), "4: Rigid Areas", b ); p.setLayout( new BoxLayout( p, BoxLayout.X_AXIS ) ); p.add( Box.createRigidArea(horzRigidAreaDim) ); p.add( new JButton("L") ); p.add( Box.createRigidArea(horzRigidAreaDim) ); p.add( new JButton("M") ); p.add( Box.createRigidArea(horzRigidAreaDim) ); p.add( new JButton("R") ); c.gridy = 3; f.getContentPane().add( p, c ); addTitledBorder( p = new JPanel(), "5: Nothing", b ); p.setLayout( new BoxLayout(p, BoxLayout.Y_AXIS ) ); p.add( new JButton("T") ); p.add( new JButton("M") ); p.add( new JButton("B") ); c.gridx = 1; c.gridy = 0; c.weightx = 1.0; c.weighty = 1.0; c.gridheight = GridBagConstraints.REMAINDER; f.getContentPane().add( p, c ); addTitledBorder( p = new JPanel(), "6: Glue", b ); p.setLayout( new BoxLayout(p, BoxLayout.Y_AXIS ) ); p.add( Box.createGlue() ); p.add( new JButton("T") ); p.add( Box.createGlue() ); p.add( new JButton("M") ); p.add( Box.createGlue() ); p.add( new JButton("B") ); p.add( Box.createGlue() ); c.gridx = 2; f.getContentPane().add( p, c ); addTitledBorder( p = new JPanel(), "7: Struts", b ); p.setLayout( new BoxLayout(p, BoxLayout.Y_AXIS ) ); p.add( Box.createVerticalStrut(strutHeight) ); p.add( new JButton("T") ); p.add( Box.createVerticalStrut(strutHeight) ); p.add( new JButton("M") ); p.add( new JButton("B") ); c.gridx = 3; f.getContentPane().add( p, c ); addTitledBorder( p = new JPanel(), "8: Rigid", b ); p.setLayout( new BoxLayout(p, BoxLayout.Y_AXIS ) ); p.add( Box.createRigidArea(vertRigidAreaDim) ); p.add( new JButton("T") ); p.add( new JButton("M") ); p.add( Box.createRigidArea(vertRigidAreaDim) ); p.add( new JButton("B") ); c.gridx = 4; f.getContentPane().add( p, c ); f.setSize( 550, 300 ); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { System.exit(0); } } ); f.setVisible(true); } }