Source of VisibilityDemo.java


  1: import javax.swing.JButton;
  2: import javax.swing.JFrame;
  3: import javax.swing.JLabel;
  4: import javax.swing.JPanel;
  5: import javax.swing.WindowConstants;
  6: import java.awt.BorderLayout;
  7: import java.awt.Color;
  8: import java.awt.Container;
  9: import java.awt.FlowLayout;
 10: import java.awt.event.ActionEvent;
 11: import java.awt.event.ActionListener;
 12: 
 13: public class VisibilityDemo extends JFrame
 14:                             implements ActionListener
 15: {
 16:     public static final int WIDTH = 300;
 17:     public static final int HEIGHT = 200;
 18: 
 19:     private JLabel upLabel;
 20:     private JLabel downLabel;
 21: 
 22:     public VisibilityDemo( )
 23:     {
 24:         setSize(WIDTH, HEIGHT);
 25:         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 26:         setTitle("Visibility Demonstration");
 27:         Container contentPane = getContentPane( );
 28:         contentPane.setLayout(new BorderLayout( ));
 29:         contentPane.setBackground(Color.WHITE);
 30: 
 31:         upLabel = new JLabel("Here I am up here!");
 32:         contentPane.add(upLabel, BorderLayout.NORTH);
 33:         upLabel.setVisible(false);
 34:         downLabel = new JLabel("Here I am down here!");
 35:         contentPane.add(downLabel, BorderLayout.SOUTH);
 36:         downLabel.setVisible(false);
 37: 
 38:         JPanel buttonPanel = new JPanel( );
 39:         buttonPanel.setBackground(Color.WHITE);
 40:         buttonPanel.setLayout(new FlowLayout( ));
 41:         JButton upButton = new JButton("Up");
 42:         upButton.addActionListener(this);
 43:         buttonPanel.add(upButton);
 44:         JButton downButton = new JButton("Down");
 45:         downButton.addActionListener(this);
 46:         buttonPanel.add(downButton);
 47:         contentPane.add(buttonPanel, BorderLayout.CENTER);
 48:     }
 49: 
 50:     public void actionPerformed(ActionEvent e)
 51:     {
 52:                 if (e.getActionCommand( ).equals("Up"))
 53:                 {
 54:                         upLabel.setVisible(true);
 55:                         downLabel.setVisible(false);
 56:                         validate( ); //Update graphics
 57:                 }
 58:                 else if (e.getActionCommand( ).equals("Down"))
 59:                 {
 60:                         downLabel.setVisible(true);
 61:                         upLabel.setVisible(false);
 62:                         validate( ); //Update graphics
 63:                 }
 64:                 else
 65:                     System.out.println("Error in VisibilityDemo interface.");
 66:     }
 67: 
 68:     public static void main(String[] args)
 69:     {
 70:                 VisibilityDemo demoGui = new VisibilityDemo( );
 71:         demoGui.setVisible(true);
 72:     }
 73: }