Source of VisibilityDemoSTQ52.java


  1: //VisibilityDemoSTQ52.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:  * Solution to Self-Test Question 52.
 16:  */
 17: public class VisibilityDemoSTQ52 extends JApplet implements ActionListener
 18: {
 19:     private JButton aButton;
 20:     private JLabel response;
 21:     private Container contentPane;
 22: 
 23:     public void init()
 24:     {
 25:         contentPane = getContentPane();
 26:         contentPane.setBackground(Color.WHITE);
 27: 
 28:         //Create button:
 29:         aButton = new JButton("Push me!");
 30:         aButton.addActionListener(this);
 31: 
 32:         //Create label:
 33:         response = new JLabel("Thanks. That felt good!");
 34:         ImageIcon smileyFaceIcon = new ImageIcon("smiley.gif");
 35:         response.setIcon(smileyFaceIcon);
 36:         response.setVisible(false); //Invisible until button is clicked
 37: 
 38:         //Add button:
 39:         contentPane.setLayout(new FlowLayout());
 40:         contentPane.add(aButton);
 41: 
 42:         //Add label
 43:         contentPane.add(response);
 44:     }
 45: 
 46:     public void actionPerformed
 47:     (
 48:         ActionEvent e
 49:     )
 50:     {
 51:         contentPane.setBackground(Color.PINK);
 52:         response.setVisible(true);//Show label
 53:         aButton.setVisible(false);//Hide button
 54:     }
 55: }