Source of VisibilityDemo.java


  1: import javax.swing.ImageIcon;
  2: import javax.swing.JApplet;
  3: import javax.swing.JButton;
  4: import javax.swing.JLabel;
  5: import java.awt.Color;
  6: import java.awt.Container;
  7: import java.awt.FlowLayout;
  8: import java.awt.Graphics;
  9: import java.awt.event.ActionEvent;
 10: import java.awt.event.ActionListener;
 11: /**
 12:  Simple demonstration of changing visibility in an applet.
 13: */
 14: public class VisibilityDemo extends JApplet implements ActionListener
 15: {
 16:     private JLabel response;
 17:     private Container contentPane;
 18: 
 19:     public void init( )
 20:     {
 21:         contentPane = getContentPane( );
 22:         contentPane.setBackground(Color.WHITE);
 23: 
 24:         //Create button:
 25:         JButton aButton = new JButton("Push me!");
 26:         aButton.addActionListener(this);
 27: 
 28:         //Create label:
 29:         response = new JLabel("Thanks. That felt good!");
 30:         ImageIcon smileyFaceIcon = new ImageIcon("smiley.gif");
 31:         response.setIcon(smileyFaceIcon);
 32:         response.setVisible(false);//Invisible until button is clicked
 33: 
 34:         //Add button:
 35:         contentPane.setLayout(new FlowLayout( ));
 36:         contentPane.add(aButton);
 37: 
 38:         //Add label
 39:         contentPane.add(response);
 40:     }
 41: 
 42:     public void actionPerformed(ActionEvent e)
 43:     {
 44:         contentPane.setBackground(Color.PINK);
 45:         response.setVisible(true);//Show label
 46:     }
 47: }