Source of ButtonIconDemo.java


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