天天看點

java 監聽機制模拟(JButton按鈕監聽機制)

一、概念

1.事件監聽器

事件監聽器就我個人的了解就是:被外部事件(鍵盤、滑鼠)引發的程式,這段程式是被嵌入到事件源的類裡面,它負責監聽事件清單。而很多人把事件監聽器了解成是實作EventListener接口的類。

而我的了解是實作EventListener接口的類是事件處理器。裡邊有處理事件的方法。從邏輯上看是這樣的,但是人家既然這樣來命名了,那也沒有辦法。因為程式員隻要知道這麼去添加監聽器就行了,不必了解内部的處理流程,但是作為一個熱愛計算機的程式員來說,必須要了解其過程。

事件監聽器的功能:

       負責監聽事件注冊類表,有相關事件注冊就立馬 new 一個事件,然後調用事件處理器裡的事件處理方法,完成事件處理。然後移除事件注冊清單的相關事件。

2.事件源:

事件源是事件的起源,可以稱作事件觸發源。其主要的功能是,介紹外邊事件,比如鍵盤、滑鼠等,當有事件時就會觸發事件監聽器。

主成分:主要由事件監聽器、注冊事件方法(如:addActionListener)構成。

3.事件對象:

實作EventObject接口的類。裡面封裝了對事件源進行操作的方法。比如:getActionCommand()方法。

4.事件處理器

       事件處理器是對事件進行處理的類,這類實作EventListener接口。此類由程式員編寫。比如 事件處理器中的處理程式:

java 監聽機制模拟(JButton按鈕監聽機制)

二、模拟程式

以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