Source of ButtonIconDemo2.java


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