C:\Users\notebook\Documents\NetBeansProjects\CS2\src\frames\KFrame6.java
 1 /*
 2  * Frame for the GUI6 program.
 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 notebook
18  */
19 // modeling the featured frame of the GUI
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     // Add the listeners to the frame
52     private void addListeners() {
53         blueButton.addActionListener(this);
54         redButton.addActionListener(this);
55         greenButton.addActionListener(this);
56         yellowButton.addActionListener(this);
57     }
58 
59     // This method serves to implement the ActionListener interfaces
60     public void actionPerformed(ActionEvent event) {
61         String command = event.getActionCommand();
62         if (command.equals("Red")) {
63             reflector.setBackground(Color.RED);
64         } else if (command.equals("Yellow")) {
65             reflector.setBackground(Color.YELLOW);
66         } else if (command.equals("Blue")) {
67             reflector.setBackground(Color.BLUE);
68         } else if (command.equals("Green")) {
69             reflector.setBackground(Color.GREEN);
70         }
71     }
72 
73 }
74