Source of ButtonIconDemo2.java


  1: 
  2: import javax.swing.*;
  3: import java.awt.*;
  4: import java.awt.event.*;
  5: 
  6: /**
  7:  Simple demonstration of putting buttons in an Applet.
  8:  These buttons do something when clicked.
  9: */
 10: public class ButtonIconDemo2 extends JApplet implements ActionListener
 11: {
 12: 
 13:     public void init( )
 14:     {
 15:         Container contentPane = getContentPane( );
 16:         contentPane.setBackground(Color.WHITE);
 17: 
 18:         contentPane.setLayout(new FlowLayout( ));
 19: 
 20:         JButton sunnyButton = new JButton("Sunny");
 21:                 ImageIcon smileyFaceIcon = new ImageIcon("smiley.gif");
 22:                 sunnyButton.setIcon(smileyFaceIcon);
 23:                 contentPane.add(sunnyButton);
 24:         sunnyButton.addActionListener(this);
 25: 
 26:         JButton cloudyButton = new JButton("Cloudy");
 27:                 ImageIcon nastyFaceIcon = new ImageIcon("nasty.gif");
 28:                 cloudyButton.setIcon(nastyFaceIcon);
 29:                 contentPane.add(cloudyButton);
 30:         cloudyButton.addActionListener(this);
 31:     }
 32: 
 33:     public void actionPerformed(ActionEvent e)
 34:     {
 35:        Container contentPane = getContentPane( );
 36: 
 37:        if (e.getActionCommand( ).equals("Sunny"))
 38:            contentPane.setBackground(Color.BLUE);
 39:        else if (e.getActionCommand( ).equals("Cloudy"))
 40:            contentPane.setBackground(Color.GRAY);
 41:        else
 42:            System.out.println("Error in button interface.");
 43:     }
 44: }
 45: