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