2009-07-30 38 views
6

如何引发Java中的自定义事件和处理。一些链接将会有所帮助。Java中的事件引发处理

谢谢。

+0

你应该尝试详细阐述一下你在做什么。什么样的事件? Java中没有内在事件,也许异常是你所追求的? – falstro 2009-07-30 07:34:02

+0

你的意思是AWT事件吗? – 2009-07-30 07:34:34

+0

像这样 http://www.codeproject.com/KB/cs/csevents01.aspx – bdhar 2009-07-30 07:37:44

回答

0

Java对事件处理没有语言支持。但是有些课程可以提供帮助。你可以看看java.awt.Event class; java.awt.eventjava.beans包。第一个包是AWT和Swing GUI库中事件处理的基础。 java.beans包包含支持Java Beans specification的东西,包括属性更改事件和bean上下文事件。

通常,事件处理是根据观测器实现或发布/订阅的模式(如由kgiannakakis提及)

15

有在Java中没有一流事件。所有事件处理都是使用接口和侦听器模式完成的。例如:

// Roughly analogous to .NET EventArgs 
class ClickEvent extends EventObject { 
    public ClickEvent(Object source) { 
    super(source); 
    } 
} 

// Roughly analogous to .NET delegate 
interface ClickListener extends EventListener { 
    void clicked(ClickEvent e); 
} 

class Button { 
    // Two methods and field roughly analogous to .NET event with explicit add and remove accessors 
    // Listener list is typically reused between several events 

    private EventListenerList listenerList = new EventListenerList(); 

    void addClickListener(ClickListener l) { 
    clickListenerList.add(ClickListener.class, l) 
    } 

    void removeClickListener(ClickListener l) { 
    clickListenerList.remove(ClickListener.class, l) 
    } 

    // Roughly analogous to .net OnEvent protected virtual method pattern - 
    // call this method to raise the event 
    protected void fireClicked(ClickEvent e) { 
    ClickListener[] ls = listenerList.getListeners(ClickEvent.class); 
    for (ClickListener l : ls) { 
     l.clicked(e); 
    } 
    } 
} 

客户代码通常使用匿名内部类注册处理程序:

Button b = new Button(); 
b.addClickListener(new ClickListener() { 
    public void clicked(ClickEvent e) { 
    // handle event 
    } 
}); 
5

的Java缺乏内在的事件处理,但也有库,帮助你做到这一点。查看javaEventing,http://code.google.com/p/javaeventing/ 它的工作原理与C#中的相似,您首先定义事件,然后注册事件侦听器。您使用EventManager.triggerEvent(.. someEvent)触发事件。它允许您为事件提供自定义条件和有效载荷。

鲍勃

2

如果你正在寻找一个.NET类型的委托时,我提出这个模板化的解决方案。 它的优点是不需要强制转换,只要使用不同的事件类,侦听器就可以实现多个“事件”。

import java.util.EventListener; 
import java.util.EventObject; 
// replaces the .net delegate 

public interface GenericEventListener<EventArgsType extends EventObject> 
    extends EventListener { 
    public void eventFired(EventArgsType e); 
} 

//------------------------------------------------ 

import java.util.EventObject; 
import java.util.Vector; 
// replaces the .net Event keyword 

public class GenericEventSource<EventArgsType extends EventObject> { 
    private Vector<GenericEventListener<EventArgsType>> listenerList = 
     new Vector<GenericEventListener<EventArgsType>>(); 

    //TODO handle multi-threading lock issues 
    public void addListener(GenericEventListener<EventArgsType> listener) { 
     listenerList.add(listener); 
    } 

    //TODO handle multi-threading lock issues 
    public void raise(EventArgsType e) { 
     for (GenericEventListener<EventArgsType> listener : listenerList) { 
      listener.eventFired(e); 
     } 
    } 
} 

//------------------------------------------------ 

// like a .net class extending EventArgs 
public class MyCustomEventArgs extends EventObject { 
    private int arg; 
    public MyCustomEventArgs(Object source, int arg) { 
     super(source); 
     this.arg = arg; 
    } 
    public int getArg() { 
     return arg; 
    } 
} 

//------------------------------------------------ 

// replaces the .net event handler function. Can be put in a nested class, Java style 
// Listener can handle several event types if they have different EventArg classes 
public class MyCustomListener implements GenericEventListener<MyCustomEventArgs> { 
    private Object source = null; 
    private int arg; 
    public void eventFired(MyCustomEventArgs e) { 
     source = e.getSource(); 
     arg = e.getArg(); 
     // continue handling event... 
    } 
} 

//------------------------------------------------ 

import GenericEventListener; 
import GenericEventSource; 
// this is the class that would like to throw events (e.g. MyButton) 
public class MyCustomEventSource { 
    // This is like declaring a .net public Event field of a specific delegate type 
    GenericEventSource<MyCustomEventArgs> myEvent = new GenericEventSource<MyCustomEventArgs>(); 

    public GenericEventSource<MyCustomEventArgs> getEvent() { 
     return myEvent; 
    } 
} 

//------------------------------------------------ 

// Examples of using the event 
MyCustomListener myListener1 = new MyCustomListener(); 
MyCustomEventSource mySource = new MyCustomEventSource(); 
mySource.getEvent().addListener(myListener1); 
mySource.getEvent().raise(new MyCustomEventArgs(mySource,5));