Java事件模型

 1 import javax.swing.*;
 2 import java.awt.event.*;
 3 public class TestSourceListener {
 4 
 5     public static void main(String[] args) {
 6         JFrame frame = new JFrame("TestSourceListener");
 7         JButton jbt = new JButton("OK");
 8         
 9         //Create a source object源对象
10         frame.add(jbt);
11         frame.setSize(200, 200);
12         frame.setVisible(true);
13         
14         //Create listener 监听器组件
15         MyListener listener = new MyListener();
16 
17         //Register listeners 注册监听器组件
18         jbt.addActionListener(listener);
19     }
20 }
21 //监听器对象
22 class MyListener implements ActionListener{
23     //ActionEvent 事件类
24     public void actionPerformed(ActionEvent e){
25         System.out.println("I will process it!");
26     }
27 }

 

 1 /**
 2  * 创建自定义源组件
 3  * 源组件必须有适当的注册与注销方法,用来添加和删除监听器。
 4  * 源组件包含特定的代码,可以创建事件对象,以及通过传递这个事件对象去调用监听器的处理器。
 5  */
 6 import java.util.*;
 7 import java.awt.event.*;
 8 public class CourseWithActionEvent {
 9     private String courseName = "default name";
10     private ArrayList<String> students = new ArrayList<String>();
11     private int enrollmentCap = 10;
12     
13     private ArrayList<ActionListener>actionlistenerList;
14     
15     private CourseWithActionEvent() {
16     }
17     public CourseWithActionEvent(String courseName){
18         this.courseName = courseName;
19     }
20     public void addStudent(String student){
21         students.add(student);
22         
23         if (students.size() > enrollmentCap) {
24             //创建事件对象
25             processEvent(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
26         }
27     }
28     
29     public String[] getStudents(){
30         return (String[])students.toArray();
31     }
32     
33     public int getNumberOfStudents(){
34         return students.size();
35     }
36     
37     public int getEnrollmentCap(){
38         return enrollmentCap;
39     }
40     
41     public void setEnrollmentCap(int enrollmentCap){
42         this.enrollmentCap = enrollmentCap;
43     }
44     
45     public synchronized void addActionListener(ActionListener listener){
46         if(actionlistenerList == null){
47             actionlistenerList = new ArrayList<ActionListener>(2);
48         }
49         if(!actionlistenerList.contains(listener)){
50             actionlistenerList.add(listener);
51         }
52     }
53     
54     public synchronized void removeActionListener(ActionListener listener) {
55         if(actionlistenerList != null && actionlistenerList.contains(listener))
56             actionlistenerList.remove(listener);
57     }
58     
59     private void processEvent(ActionEvent e) {
60         ArrayList list;
61         
62         synchronized (this) {
63             if(actionlistenerList == null) return;
64             list = (ArrayList)actionlistenerList.clone();
65         }
66         //通过调用每个监听器的actionPerformed方法来通知actionlistenerList中的监听器进行处理。
67         for (int i = 0; i < list.size(); i++) {
68             ActionListener listener = (ActionListener)list.get(i);
69             listener.actionPerformed(e);
70         }
71     }
72 }

 

posted @ 2013-11-30 10:29  soul390  阅读(250)  评论(0)    收藏  举报