实践证明:当类想实现两个监听接口的时候,必须把两个都设置成内部类,不可能一个是外部类实现,一个是内部类实现。这样容易捕获错误,出现异常。

最初的程序出现了错误,就是因为我把两个监听接口,一个是县委内部类,一个实现为外部类,导致监听时出现错误。把外部类屏蔽以后,内部类才可以正常工作。如下所示:

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 import javax.swing.*;
 4 
 5 public class TextArea1 //implements ActionListener{
 6 {
 7     JTextArea text;
 8     JCheckBox check;
 9     
10     public static void main(String[] args){
11         TextArea1 gui=new TextArea1();
12         gui.go();
13     }
14     public void go(){
15         JFrame frame=new JFrame();
16         JPanel panel=new JPanel();
17         JButton button=new JButton("Just Clicked It");
18         //button.addActionListener(this);
19         text=new JTextArea(10,20);
20         text.setLineWrap(true);
21         
22         
23         JScrollPane scroller=new JScrollPane(text);
24         scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
25         scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
26         
27         check=new JCheckBox("Goes to 11");
28         
29         check.addItemListener(new CheckListener());
30         
31         
32         panel.add(scroller);
33         
34         frame.getContentPane().add(BorderLayout.CENTER,panel);
35         frame.getContentPane().add(BorderLayout.SOUTH,button);
36         frame.getContentPane().add(BorderLayout.NORTH,check);
37         
38         frame.setSize(300,300);
39         frame.setVisible(true);
40     }
41     /*public void actionPerformed(ActionEvent ev){
42         text.append("button clicked \n");
43     }*/
44     class CheckListener implements ItemListener{
45         public void itemStateChanged(ItemEvent ev){
46             String onOrOff="off";
47             if(check.isSelected()) onOrOff="on";
48             System.out.println("Check box is "+onOrOff);
49       }
50     }
51 }

通过·设置内部类,来继承和实现ActionListener,也可以实现,程序代码如下图所示:

 1 import java.awt.*;
 2 import java.awt.event.*;
 3 import javax.swing.*;
 4 
 5 public class TextArea1 {
 6 
 7     JTextArea text;
 8     JCheckBox check;
 9     
10     public static void main(String[] args){
11         TextArea1 gui=new TextArea1();
12         gui.go();
13     }
14     public void go(){
15         JFrame frame=new JFrame();
16         JPanel panel=new JPanel();
17         JButton button=new JButton("Just Clicked It");
18         button.addActionListener(new ButtonListener());
19         text=new JTextArea(10,20);
20         text.setLineWrap(true);
21         
22         
23         JScrollPane scroller=new JScrollPane(text);
24         scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
25         scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
26         
27         check=new JCheckBox("Goes to 11");
28         
29         check.addItemListener(new CheckListener());
30         
31         
32         panel.add(scroller);
33         
34         frame.getContentPane().add(BorderLayout.CENTER,panel);
35         frame.getContentPane().add(BorderLayout.SOUTH,button);
36         frame.getContentPane().add(BorderLayout.NORTH,check);
37         
38         frame.setSize(300,300);
39         frame.setVisible(true);
40     }
41     /*public void actionPerformed(ActionEvent ev){
42         text.append("button clicked \n");
43     }*/
44     class ButtonListener implements ActionListener{
45         public void actionPerformed(ActionEvent ev){
46             text.append("button clicked \n");
47         }
48     }
49     class CheckListener implements ItemListener{
50         public void itemStateChanged(ItemEvent ev){
51             String onOrOff="off";
52             if(check.isSelected()) onOrOff="on";
53             System.out.println("Check box is "+onOrOff);
54       }
55     }
56 }

 

posted @ 2013-08-09 11:48  婷婷玉立的成长之家  阅读(329)  评论(0编辑  收藏  举报