Source of ButtonDemo.java


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