C:\Users\notebook\Documents\NetBeansProjects\CS2\src\gui\GUI5.java |
48
49 private class KFrame extends JFrame implements ActionListener {
50
51 private JButton blueButton;
52 private JButton redButton;
53 private JButton greenButton;
54 private JButton yellowButton;
55 private JPanel reflector;
56
57 public KFrame(String title) {
58 super(title);
59 setSize(500, 300);
60 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
61 addComponents(getContentPane());
62 addListeners();
63 setVisible(true);
64 }
65
66 private void addComponents(Container contentPane) {
67 blueButton = new JButton("Blue");
68 redButton = new JButton("Red");
69 greenButton = new JButton("Green");
70 yellowButton = new JButton("Yellow");
71 reflector = new JPanel();
72 contentPane.setLayout(new BorderLayout());
73 contentPane.add(blueButton, BorderLayout.NORTH);
74 contentPane.add(redButton, BorderLayout.SOUTH);
75 contentPane.add(greenButton, BorderLayout.EAST);
76 contentPane.add(yellowButton, BorderLayout.WEST);
77 contentPane.add(reflector, BorderLayout.CENTER);
78 }
79
80
81 private void addListeners() {
82 blueButton.addActionListener(this);
83 redButton.addActionListener(this);
84 greenButton.addActionListener(this);
85 yellowButton.addActionListener(this);
86 }
87
88
89 @Override
90 public void actionPerformed (ActionEvent event) {
91 String command = event.getActionCommand();
92 if ( command.equals("Red") ) {
93 reflector.setBackground(Color.RED);
94 } else if ( command.equals("Yellow") ) {
95 reflector.setBackground(Color.YELLOW);
96 } else if (command.equals("Blue")) {
97 reflector.setBackground(Color.BLUE);
98 } else if ( command.equals("Green") ) {
99 reflector.setBackground(Color.GREEN);
100 }
101 }
102
103 }
104
105 }