public class BoxLayoutDemo extends JFrame
1: import javax.swing.Box;
2: import javax.swing.BoxLayout;
3: import javax.swing.JButton;
4: import javax.swing.JFrame;
5: import javax.swing.JPanel;
6: import java.awt.BorderLayout;
7: import java.awt.Color;
8: import java.awt.Component;
9: import java.awt.Container;
10: import java.awt.event.ActionEvent;
11: import java.awt.event.ActionListener;
13: /**
14: Simple demonstration of BoxLayout manager class and the use of
15: struts to separate components (in this case, buttons). (For an
16: alternative implementation, see BoxClassDemo in Listing 15.6.)
17: */
18: public class BoxLayoutDemo extends JFrame
19: implements ActionListener
20: {
21: public static final int WIDTH = 300;
22: public static final int HEIGHT = 200;
23: public static final int HORIZONTAL_STRUT_SIZE = 15;
24: public static final int VERTICAL_STRUT_SIZE = 10;
25:
26: private JPanel colorPanel;
27:
28: public BoxLayoutDemo( )
29: {
30: setSize(WIDTH, HEIGHT);
31: addWindowListener(new WindowDestroyer( ));
32: setTitle("Box Demonstration");
33: Container content = getContentPane( );
34: content.setLayout(new BorderLayout( ));
35:
36: colorPanel = new JPanel( );
37: colorPanel.setBackground(Color.BLUE);
38: content.add(colorPanel, BorderLayout.CENTER);
39:
40: //Horizontal buttons at bottom of frame:
41: JPanel horizontalPanel = new JPanel( );
42: horizontalPanel.setLayout(
43: new BoxLayout(horizontalPanel, BoxLayout.X_AXIS));
44: Component horizontalStrut =
45: Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
46: horizontalPanel.add(horizontalStrut);
47:
48: JButton hStopButton = new JButton("Red");
49: hStopButton.addActionListener(this);
50: horizontalPanel.add(hStopButton);
51:
52: Component horizontalStrut2 =
53: Box.createHorizontalStrut(HORIZONTAL_STRUT_SIZE);
54: horizontalPanel.add(horizontalStrut2);
55:
56: JButton hGoButton = new JButton("Green");
57: hGoButton.addActionListener(this);
58: horizontalPanel.add(hGoButton);
59:
60: content.add(horizontalPanel, BorderLayout.SOUTH);
61:
62: //Vertical buttons on right side of frame:
63: JPanel verticalPanel = new JPanel( );
64: verticalPanel.setLayout(
65: new BoxLayout(verticalPanel, BoxLayout.Y_AXIS));
66: Component verticalStrut =
67: Box.createVerticalStrut(VERTICAL_STRUT_SIZE);
68: verticalPanel.add(verticalStrut);
69:
70: JButton vStopButton = new JButton("Red");
71: vStopButton.addActionListener(this);
72: verticalPanel.add(vStopButton);
73:
74: Component verticalStrut2 =
75: Box.createVerticalStrut(VERTICAL_STRUT_SIZE);
76: verticalPanel.add(verticalStrut2);
77:
78: JButton vGoButton = new JButton("Green");
79: vGoButton.addActionListener(this);
80: verticalPanel.add(vGoButton);
81:
82: content.add(verticalPanel, BorderLayout.EAST);
83: }
84:
85: public void actionPerformed(ActionEvent e)
86: {
87: if (e.getActionCommand( ).equals("Red"))
88: colorPanel.setBackground(Color.RED);
89: else if (e.getActionCommand( ).equals("Green"))
90: colorPanel.setBackground(Color.GREEN);
91: else
92: System.out.println("Error in button interface.");
93: }
94:
95: public static void main(String[] args)
96: {
97: BoxLayoutDemo gui = new BoxLayoutDemo( );
98: gui.setVisible(true);
99: }
100: }