public class ButtonDemo extends JFrame implements ActionListener
1:
2: import javax.swing.JButton;
3: import javax.swing.JFrame;
4: import java.awt.Color;
5: import java.awt.Container;
6: import java.awt.FlowLayout;
7: import java.awt.event.ActionEvent;
8: import java.awt.event.ActionListener;
9:
10: /**
11: Simple demonstration of putting buttons in a JFrame window.
12: */
13: public class ButtonDemo extends JFrame implements ActionListener
14: {
15: public static final int WIDTH = 400;
16: public static final int HEIGHT = 300;
17:
18: public ButtonDemo( )
19: {
20: setSize(WIDTH, HEIGHT);
21: WindowDestroyer listener = new WindowDestroyer( );
22: addWindowListener(listener);
23:
24: Container contentPane = getContentPane( );
25: contentPane.setBackground(Color.WHITE);
26:
27: contentPane.setLayout(new FlowLayout( ));
28:
29: JButton sunnyButton = new JButton("Sunny");
30: sunnyButton.addActionListener(this);
31: contentPane.add(sunnyButton);
32:
33: JButton cloudyButton = new JButton("Cloudy");
34: cloudyButton.addActionListener(this);
35: contentPane.add(cloudyButton);
36: }
37:
38: public void actionPerformed(ActionEvent e)
39: {
40: String actionCommand = e.getActionCommand( );
41: Container contentPane = getContentPane( );
42:
43: if (actionCommand.equals("Sunny"))
44: contentPane.setBackground(Color.BLUE);
45: else if (actionCommand.equals("Cloudy"))
46: contentPane.setBackground(Color.GRAY);
47: else
48: System.out.println("Error in button interface.");
49: }
50: }
51:
52: