Source of GlueDemo.java


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