Java Reference
In-Depth Information
Display 17.16
Listeners as Inner Classes (part 2 of 3)
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()
The resulting GUI is the same as in
Display 17.14.
35
{
36
super ("Menu Demonstration");
37
setSize(WIDTH, HEIGHT);
38
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
39
setLayout( new GridLayout(1, 3));
40
greenPanel = new JPanel();
41
greenPanel.setBackground(Color.LIGHT_GRAY);
42
add(greenPanel);
43
whitePanel = new JPanel();
44
whitePanel.setBackground(Color.LIGHT_GRAY);
45
add(whitePanel);
46
grayPanel = new JPanel();
47
grayPanel.setBackground(Color.LIGHT_GRAY);
48
add(grayPanel);
49
JMenu colorMenu = new JMenu("Add Colors");
50
JMenuItem greenChoice = new JMenuItem("Green");
51
greenChoice.addActionListener( new greenListener());
52
colorMenu.add(greenChoice);
53
JMenuItem whiteChoice = new JMenuItem("White");
54
whiteChoice.addActionListener( new WhiteListener());
55
colorMenu.add(whiteChoice);
Search WWH ::




Custom Search