java 监听机制模拟(JButton按钮监听机制)

一、概念

1.事件监听器

事件监听器就我个人的理解就是:被外部事件(键盘、鼠标)引发的程序,这段程序是被嵌入到事件源的类里面,它负责监听事件列表。而很多人把事件监听器理解成是实现EventListener接口的类。

而我的理解是实现EventListener接口的类是事件处理器。里边有处理事件的方法。从逻辑上看是这样的,但是人家既然这样来命名了,那也没有办法。因为程序员只要知道这么去添加监听器就行了,不必理解内部的处理流程,但是作为一个热爱计算机的程序员来说,必须要理解其过程。

事件监听器的功能:

       负责监听事件注册类表,有相关事件注册就立马 new 一个事件,然后调用事件处理器里的事件处理方法,完成事件处理。然后移除事件注册列表的相关事件。

2.事件源:

事件源是事件的起源,可以称作事件触发源。其主要的功能是,介绍外边事件,比如键盘、鼠标等,当有事件时就会触发事件监听器。

主成分:主要由事件监听器、注册事件方法(如:addActionListener)构成。

3.事件对象:

实现EventObject接口的类。里面封装了对事件源进行操作的方法。比如:getActionCommand()方法。

4.事件处理器

       事件处理器是对事件进行处理的类,这类实现EventListener接口。此类由程序员编写。比如 事件处理器中的处理程序:

二、模拟程序

以JButton按钮为列,看一下程序:

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
import java.util.EventListener;
import java.util.EventObject;
import java.util.Vector;
 
//自定义一个按钮事件
class ActionEvent extends EventObject{
 
String actionCommand;
public ActionEvent(Object source,String command)
{
super(source);
this.actionCommand = command;
System.out.println("按钮点击事件产生!");
}
 
public String getActionCommand() {
return actionCommand;
}
}
 
// 弄一个在按钮里面使用的接口,通过继承此接口可以完成事件处理的类
interface ActionListener extends EventListener{
//这里是当事件发生后的响应过程
public void actionPerformed(ActionEvent me);
}
 
//再自定义一个监听器
class MyListener implements ActionListener
{
//这里是当事件发生后的响应过程
public void actionPerformed(ActionEvent me)
{
String str=me.getActionCommand();
if(str.equals("hello"))
System.out.println("按钮点击事件被处理");
 
}
}
 
//以下这个类为触发事件的事件源
class JButton { //比如button按钮
 
String s;
 
JButton(String s)
{
this.s=s;
}
String getAt()
{
return s;
}
private Vector vectorListeners=new Vector(); //定义一个向量
 
public synchronized void addActionListener(ActionListener actionlistener) //注册监听器
{
vectorListeners.addElement(actionlistener); // 将指定元素添加到此向量的末尾
System.out.println("按钮上注册监听器");
}
 
public synchronized void removeActionListener(ActionListener actionlistener) //移除监听器
{
vectorListeners.removeElement(actionlistener); //从此向量中移除变量的第一个(索引最小的)匹配项。
System.out.println("移除按钮上的监听器");
}
 
protected void activateMyEvent() //判断监听器是否监听到
{
Vector tempVector=null;
 
synchronized(this)
{
ActionEvent e;
e=new ActionEvent(this,getAt()); //产生了事件
tempVector=(Vector)vectorListeners.clone();
for(int i=0;i<tempVector.size();i++)
{
ActionListener actionlistener=(ActionListener)tempVector.elementAt(i); //调用监听器处理事件
actionlistener.actionPerformed(e); //处理事件
removeActionListener(actionlistener); //移除监听器
}
}
}
 
//定义一个公用方法用于触发事件 安装在按钮内部
public void Trigger()
{
System.out.println("按钮点击事件触发");
activateMyEvent();
}
}
 
public class Test {
 
public static void main(String[] args)
{
JButton button=new JButton("hello"); //事件源-->按钮
MyListener mylistener= new MyListener(); //定义监听器
 
button.addActionListener(mylistener); //在按钮上注册监听器监听按钮点击事件
button.Trigger(); //触发按钮事件
 
System.out.println("程序结束");
}
}
 来自CODE的代码片
ButtonEven.java

三、问题:

(理解以上概念及代码后,在看看以下问题)

1、以上只是模拟,那么Java中是如何通过点击按钮,然后触发按钮事件的,比如点击按钮就触发以上程序所提供的Trigger() 方法的?

原文地址:https://www.cnblogs.com/jiangzhaowei/p/7445626.html