Source of VisibilityDemo.java


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