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