Source of ButtonIconDemo.java


  1: //ButtonIconDemo.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 ButtonIconDemo 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:         contentPane.add(cloudyButton);
 29:         cloudyButton.addActionListener(this);
 30:     }
 31: 
 32:     public void actionPerformed(ActionEvent e)
 33:     {
 34:        Container contentPane = getContentPane( );
 35: 
 36:        if (e.getActionCommand( ).equals("Sunny"))
 37:            contentPane.setBackground(Color.BLUE);
 38:        else if (e.getActionCommand( ).equals("Cloudy"))
 39:            contentPane.setBackground(Color.GRAY);
 40:        else
 41:            System.out.println("Error in button interface.");
 42:     }
 43: }
 44: