Source of VisibilityDemo.java


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