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