Java Reference
In-Depth Information
Display 17.16
Listeners as Inner Classes
(part 1 of 2)
<Import statements are the same as in Display 17.14.>
1
public class
InnerListenersDemo
extends
JFrame
2 {
3
public static final int
WIDTH = 300;
4
public static final int
HEIGHT = 200;
5
private
JPanel bluePanel;
6
private
JPanel whitePanel;
7
private
JPanel grayPanel;
8
private class
blueListener
implements
ActionListener
9 {
10
public void
actionPerformed(ActionEvent e)
11 {
12 bluePanel.setBackground(Color.BLUE);
13 }
14 }
//End of greenListener inner class
15
private class
WhiteListener
implements
ActionListener
16 {
17
public void
actionPerformed(ActionEvent e)
18 {
19 whitePanel.setBackground(Color.WHITE);
20 }
21 } //
End of WhiteListener inner class
22
private class
grayListener
implements
ActionListener
23 {
24
public void
actionPerformed(ActionEvent e)
25 {
26 grayPanel.setBackground(Color.GRAY);
27 }
28 } //
End of grayListener inner class
29
public static void
main(String[] args)
30 {
31 InnerListenersDemo gui =
new
InnerListenersDemo();
32 gui.setVisible(
true
);
33 }
34
public
InnerListenersDemo()
35 {
36
super
("Menu Demonstration");
37 setSize(WIDTH, HEIGHT);
38 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39 setLayout(
new
GridLayout(1, 3));
The resulting GUI is the same as in
Display 17.14
(continued)