Source of BoxClassDemo.java


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