/home/sjenks/NetBeansProjects/CS2/src/frames/KFrame6.java |
1
2
3
4 package frames;
5
6 import java.awt.BorderLayout;
7 import java.awt.Color;
8 import java.awt.Container;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import javax.swing.JButton;
12 import javax.swing.JFrame;
13 import javax.swing.JPanel;
14
15
16
17 @author
18
19
20 public class KFrame6 extends JFrame implements ActionListener {
21
22 private JButton blueButton;
23 private JButton redButton;
24 private JButton greenButton;
25 private JButton yellowButton;
26 private JPanel reflector;
27
28 public KFrame6 (String title) {
29 super(title);
30 setSize(500, 300);
31 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32 addComponents(getContentPane());
33 addListeners();
34 setVisible(true);
35 }
36
37 private void addComponents(Container contentPane) {
38 blueButton = new JButton("Blue");
39 redButton = new JButton("Red");
40 greenButton = new JButton("Green");
41 yellowButton = new JButton("Yellow");
42 reflector = new JPanel();
43 contentPane.setLayout(new BorderLayout());
44 contentPane.add(blueButton, BorderLayout.NORTH);
45 contentPane.add(redButton, BorderLayout.SOUTH);
46 contentPane.add(greenButton, BorderLayout.EAST);
47 contentPane.add(yellowButton, BorderLayout.WEST);
48 contentPane.add(reflector, BorderLayout.CENTER);
49 }
50
51
52 private void addListeners() {
53 blueButton.addActionListener(this);
54 redButton.addActionListener(this);
55 greenButton.addActionListener(this);
56 yellowButton.addActionListener(this);
57 }
58
59
60 @Override
61 public void actionPerformed(ActionEvent event) {
62 String command = event.getActionCommand();
63 if (command.equals("Red")) {
64 reflector.setBackground(Color.RED);
65 } else if (command.equals("Yellow")) {
66 reflector.setBackground(Color.YELLOW);
67 } else if (command.equals("Blue")) {
68 reflector.setBackground(Color.BLUE);
69 } else if (command.equals("Green")) {
70 reflector.setBackground(Color.GREEN);
71 }
72
73 }
74 }
75
76
77