// Roughly analogous to .NET EventArgs
class ClickEvent extends java.util.EventObject {
public ClickEvent(Object source) {
super(source);
}
}
// Roughly analogous to .NET delegate
interface ClickListener extends java.util.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
}
});
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));
6条答案
按热度按时间cnjp1d6j1#
Java中没有第一类事件。所有事件处理都使用接口和侦听器模式完成。例如:
客户端代码通常使用匿名内部类来注册处理程序:
g6ll5ycj2#
Java缺乏内在的事件处理,但是有一些库可以帮助你实现这一点。看看javaEventing,http://code.google.com/p/javaeventing/它的工作原理与C#类似,在C#中,你首先定义你的事件,然后注册事件侦听器。你使用EventManager.triggerEvent(..someEvent)触发事件。它还允许你为你的事件提供自定义的条件和负载。
摆锤
qcuzuvrc3#
Java不像C#那样内置对委托和事件的支持,您需要自己实现Observer或Publish/Subscribe模式。
ct2axkht4#
如果你正在寻找一个.net类型的委托事件,我建议你使用这个模板化的解决方案,它的优点是不需要硬转换,并且一个监听器可以实现多个“事件”,只要它们使用不同的事件类。
xoefb8l85#
下面的链接帮助我理解。
http://www.javaworld.com/javaworld/javaqa/2002-03/01-qa-0315-happyevent.html
yvgpqqbh6#
Java没有对事件处理的语言内支持。但是有一些类可以帮助你。你可以看看
java.awt.Event
类;java.awt.event
和java.beans
包。第一个包是AWT和Swing GUI库中事件处理的基础。java.beans
包包含Java Beans specification的支持内容,包括属性更改事件和bean上下文事件。通常,事件处理是根据Observer或Publish/Subscribe模式实现的(如kgiannakakis所述)