Source of ButtonDemo.java


  1: //ButtonDemo.java
  2: 
  3: import javax.swing.JApplet;
  4: import javax.swing.JButton;
  5: import java.awt.Color;
  6: import java.awt.Container;
  7: import java.awt.FlowLayout;
  8: import java.awt.Graphics;
  9: import java.awt.event.ActionEvent;
 10: import java.awt.event.ActionListener;
 11: 
 12: /**
 13:  * Simple demonstration of adding buttons to an applet.
 14:  * These buttons do something when clicked.
 15:  */
 16: public class ButtonDemo 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:         contentPane.add(sunnyButton);
 28:         sunnyButton.addActionListener(this);
 29: 
 30:         JButton cloudyButton = new JButton("Cloudy");
 31:         contentPane.add(cloudyButton);
 32:         cloudyButton.addActionListener(this);
 33:     }
 34: 
 35:     public void actionPerformed(ActionEvent e)
 36:     {
 37:        Container contentPane = getContentPane( );
 38: 
 39:        if (e.getActionCommand( ).equals("Sunny"))
 40:            contentPane.setBackground(Color.BLUE);
 41:        else if (e.getActionCommand( ).equals("Cloudy"))
 42:            contentPane.setBackground(Color.GRAY);
 43:        else
 44:            System.out.println("Error in button interface.");
 45:     }
 46: }
 47: