日期:2014-05-20 浏览次数:20733 次
package fEvent; import java.util.*; public class FEventArgs extends java.util.EventObject{ protected Object source; protected String[] strings; protected double[] numbers; public Object _GetSource(){ return source; } public double[] _GetNumbers(){ return numbers; } public String[] _GetStrings(){ return strings; } public FEventArgs(Object source,String[] s,double[] d) { super(source); this.source = source; strings = s; numbers = d; } }
public class FEventListener implements java.util.EventListener{ //这里是当事件发生后的响应过程 public void EventActivated(FEventArgs args) { } }
import java.util.*; public class FEvent { private Vector Listeners = new Vector(); public synchronized void Add(FEventListener l){ Listeners.addElement(l); } public synchronized void Remove(FEventListener l){ Listeners.removeElement(l); } public void Start(Object sender,String[] s,double[] d){ Vector tempVector = null; FEventArgs e = new FEventArgs(sender,s,d); synchronized(this) { tempVector = (Vector)Listeners.clone(); for(int i=0;i<tempVector.size();i++) { FEventListener l=(FEventListener)tempVector.elementAt(i); l.EventActivated(e); } } } }
FEvent fe1;//定义事件 //...... //定义事件处理类 class FL1 extends FEventListener{ public void EventActivated(FEventArgs args){ JOptionPane.showMessageDialog(null, args._GetStrings()[0]); } } //...... //添加事件 fe1 = new FEvent(); fe1.Add(new FL1()); //....... //触发事件 fe1.Start(bt_save,new String[]{"hello java!"}, null);