public class PinkJPanelDemo extends JFrame implements ActionListener
1:
2: import javax.swing.JButton;
3: import javax.swing.JFrame;
4: import java.awt.BorderLayout;
5: import java.awt.Color;
6: import java.awt.Container;
7: import java.awt.FlowLayout;
8: import java.awt.event.ActionEvent;
9: import java.awt.event.ActionListener;
10: /**
11: Simple demonstration of putting buttons in a PinkJPanel.
12: */
13: public class PinkJPanelDemo extends JFrame implements ActionListener
14: {
15: public static final int WIDTH = 300;
16: public static final int HEIGHT = 200;
17:
18: public static void main(String[] args)
19: {
20: PinkJPanelDemo guiWithPanel = new PinkJPanelDemo();
21: guiWithPanel.setVisible(true);
22: }
23:
24: public PinkJPanelDemo()
25: {
26: setSize(WIDTH, HEIGHT);
27: addWindowListener(new WindowDestroyer());
28: setTitle("Panel Demonstration");
29: Container contentPane = getContentPane();
30: contentPane.setBackground(Color.BLUE);
31: contentPane.setLayout(new BorderLayout());
32:
33: PinkJPanel buttonPanel = new PinkJPanel();
34:
35: buttonPanel.setLayout(new FlowLayout());
36:
37: JButton stopButton = new JButton("Red");
38: stopButton.setBackground(Color.RED);
39: stopButton.addActionListener(this);
40: buttonPanel.add(stopButton);
41:
42: JButton goButton = new JButton("Green");
43: goButton.setBackground(Color.GREEN);
44: goButton.addActionListener(this);
45: buttonPanel.add(goButton);
46:
47: contentPane.add(buttonPanel, BorderLayout.SOUTH);
48: }
49:
50: public void actionPerformed(ActionEvent e)
51: {
52: Container contentPane = getContentPane();
53:
54: if (e.getActionCommand().equals("Red"))
55: contentPane.setBackground(Color.RED);
56: else if (e.getActionCommand().equals("Green"))
57: contentPane.setBackground(Color.GREEN);
58: else
59: System.out.println("Error in button interface.");
60: }
61: }