/home/sjenks/NetBeansProjects/CS2/src/gui/GUI5.java
  1 /*
  2  * GUI5 is like GUI4 except that the central region is replaced by a panel which 
  3  * reflects the color of the most recently selected button. This last bit--the 
  4  * fact that the panel reflects the color, is something of a deal. The GUI 
  5  * responds! 
  6  *
  7  * Evernts are reatured in this Program! What is an event? How are events 
  8  * communicated from a component to an event handling object? Answers to 
  9  * these and other questions can be found in the given code!
 10  */
 11 package gui;
 12 
 13 import java.awt.BorderLayout;
 14 import java.awt.Color;
 15 import java.awt.Container;
 16 import java.awt.event.ActionEvent;
 17 import java.awt.event.ActionListener;
 18 import javax.swing.JButton;
 19 import javax.swing.JFrame;
 20 import javax.swing.JPanel;
 21 import javax.swing.SwingUtilities;
 22 
 23 /**
 24  *
 25  * @author sjenks
 26  */
 27 public class GUI5 {
 28 
 29     /**
 30      * @param args the command line arguments
 31      */
 32     public static void main(String[] args) {
 33         SwingUtilities.invokeLater(new ThreadForGUI());
 34     }
 35 
 36     private static class ThreadForGUI implements Runnable {
 37 
 38         public ThreadForGUI() {
 39         }
 40 
 41         @Override
 42         public void run() {
 43             GUI5 gui = new GUI5();
 44         }
 45     }
 46 
 47     public GUI5() {
 48         KFrame frame = new KFrame("GUI 5");
 49     }
 50 
 51     // modeling the featured frame of the GUI
 52     private class KFrame extends JFrame implements ActionListener {
 53 
 54         private JButton blueButton;
 55         private JButton redButton;
 56         private JButton greenButton;
 57         private JButton yellowButton;
 58         private JPanel reflector;
 59 
 60         public KFrame(String title) {
 61             super(title);
 62             setSize(500, 300);
 63             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 64             addComponents(getContentPane());
 65             addListeners();
 66             setVisible(true);
 67         }
 68 
 69         private void addComponents(Container contentPane) {
 70             blueButton = new JButton("Blue");
 71             redButton = new JButton("Red");
 72             greenButton = new JButton("Green");
 73             yellowButton = new JButton("Yellow");
 74             reflector = new JPanel();
 75             contentPane.setLayout(new BorderLayout());
 76             contentPane.add(blueButton, BorderLayout.NORTH);
 77             contentPane.add(redButton, BorderLayout.SOUTH);
 78             contentPane.add(greenButton, BorderLayout.EAST);
 79             contentPane.add(yellowButton, BorderLayout.WEST);
 80             contentPane.add(reflector, BorderLayout.CENTER);
 81         }
 82 
 83         //Add the listeners to the frame.
 84         private void addListeners() {
 85             blueButton.addActionListener(this);
 86             redButton.addActionListener(this);
 87             greenButton.addActionListener(this);
 88             yellowButton.addActionListener(this);
 89         }
 90 
 91         // This method serves to implemnt the ActionListener interface
 92         @Override
 93         public void actionPerformed(ActionEvent event) {
 94             String command = event.getActionCommand();
 95             if (command.equals("Red")) {
 96                 reflector.setBackground(Color.RED);
 97             } else if (command.equals("Yellow")) {
 98                 reflector.setBackground(Color.YELLOW);
 99             } else if (command.equals("Blue")) {
100                 reflector.setBackground(Color.BLUE);
101             } else if (command.equals("Green")) {
102                 reflector.setBackground(Color.GREEN);
103             }
104 
105         }
106     }
107 
108 }
109