【转】转帖并注释:Java中的事件处理机制--事件监听器的四种实现方式
原文地址:http://blog.sina.com.cn/s/blog_4b650d650100nqws.html
Java中四种事件监听器的实现方式分别为:
- 自身类做为事件监听器
- 外部类作为事件监听器
- 匿名内部类作为事件监听器
- 内部类作为事件监听器.
下面分别描述:
//---------------------------------------------------------------------
1. 自身类作为事件监听器:
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 //ActionListener:java.awt.event.ActionListener(interface),用来监听Action事件
6 class ThisClassEvent extends JFrame implements ActionListener
7 {
8 JButton btn;
9
10 public ThisClassEvent()
11 {
12 super("Java事件监听机制");
13 setLayout(new FlowLayout());
14 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15
16
17 btn=new JButton("点击");
18 //addActionListerner()原型为:public void addActionListener(ActionListener l)
19 btn.addActionListener(this);
20 getContentPane().add(btn);
21
22
23 setBounds(200,200,300,160);
24 setVisible(true);
25 }
26
27
28 /------------------------------
29 //actionPerformed函数是从ActionListener中继承来的.
30
31 public void actionPerformed (ActionEvent e)
32 {
33 Container c=getContentPane();
34 c.setBackground(Color.red);
35 }
36
37
38 /------------------------------
39 public static void main(String args[])
40 {
41 new ThisClassEvent();
42 }
43 }
2. 外部类作为事件监听器:
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 6 class OuterClassEvent extends JFrame 7 { 8 JButton btn; 9 public OuterClassEvent() 10 { 11 super("Java事件监听机制"); 12 setLayout(new FlowLayout()); 13 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 14 15 16 btn=new JButton("点击"); 17 18 //addActionListerner()原型为:public void addActionListener(ActionListener l) 19 //new OuterClass(this)是OuterClass的实例,而OuterClass是从ActionListener继承来的 20 btn.addActionListener(new OuterClass(this)); 21 getContentPane().add(btn); 22 23 24 setBounds(200,200,300,160); 25 setVisible(true); 26 } 27 28 29 public static void main(String args[]) 30 { 31 new OuterClassEvent(); 32 } 33 } 34 35 36 //外部类------------------------------ 37 38 //OuterClass从ActionListener继承 39 class OuterClass implements ActionListener 40 { 41 OuterClassEvent oce; 42 public OuterClass(OuterClassEvent oce) 43 { 44 this.oce = oce; 45 } 46 47 48 49 //actionPerformed函数是从ActionListener中继承来的. 50 public void actionPerformed(ActionEvent e) 51 { 52 Container c=oce.getContentPane(); 53 c.setBackground(Color.red); 54 } 55 } 56 57 58 59 //---------------------------------------------------------------------
3. 匿名内部类作为事件监听器:
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 6 class AnonymousEvent extends JFrame 7 { 8 JButton btn; 9 10 11 public AnonymousEvent() 12 { 13 super("Java事件监听机制"); 14 setLayout(new FlowLayout()); 15 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 17 18 btn=new JButton("点击"); 19 20 21 //匿名内部类--------------------------- 22 //addActionListerner()原型为:public void addActionListener(ActionListener l) 23 btn.addActionListener 24 ( 25 //new ActionListener()声明了ActionListener的一个实例 26 new ActionListener() 27 { 28 29 //actionPerformed函数是从ActionListener中继承来的. 30 public void actionPerformed(ActionEvent e) 31 { 32 Container c=getContentPane(); 33 c.setBackground(Color.red); 34 } 35 } 36 ); 37 38 //------------------------------ 39 getContentPane().add(btn); 40 setBounds(200,200,300,160); 41 setVisible(true); 42 43 } 44 45 46 public static void main(String args[]) 47 { 48 new AnonymousEvent(); 49 } 50 } 51 52 53 54 //---------------------------------------------------------------------
4.内部类作为事件监听器:
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 6 class InnerClassEvent extends JFrame 7 8 { 9 JButton btn; 10 public InnerClassEvent() 11 12 { 13 super("Java事件监听机制"); 14 setLayout(new FlowLayout()); 15 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 17 18 btn=new JButton("点击"); 19 20 //addActionListerner()原型为:public void addActionListener(ActionListener l) 21 btn.addActionListener(new InnerClass()); 22 getContentPane().add(btn); 23 24 25 setBounds(200,200,300,160); 26 setVisible(true); 27 } 28 29 30 //内部类--------------------------- 31 //InnerClass继承了ActionListener 32 class InnerClass implements ActionListener 33 { 34 35 //actionPerformed函数是从ActionListener中继承来的. 36 public void actionPerformed (ActionEvent e) 37 { 38 Container c=getContentPane(); 39 c.setBackground(Color.red); 40 } 41 } 42 43 44 //------------------------------ 45 public static void main(String args[]) 46 47 { 48 new InnerClassEvent(); 49 } 50 } 51 52 //---------------------------------------------------------------------