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