Java 中的事件侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13990798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Event Listeners in Java
提问by Wlofrevo Kcast
I've been working with event listeners in AS3, but seems like there is none in java (except for graphics components). It's surprising.
我一直在使用 AS3 中的事件侦听器,但在 Java 中似乎没有(除了图形组件)。令人惊讶。
The question is, how could i implement my own event listener in java? Maybe someone did that work before?
问题是,我如何在 Java 中实现我自己的事件侦听器?也许有人以前做过这项工作?
回答by assylias
You can define a Listener interface:
你可以定义一个监听器接口:
public interface EventListener {
void fireEvent (Event e);
}
Then in your code:
然后在你的代码中:
EventListener lst = new EventListener() {
@Override
public void fireEvent (Event e) {
//do what you want with e
}
}
someObject.setListener(lst);
someObject.somethingHappened();
Then in someObject (in practice you would probably hold a list of listeners):
然后在 someObject 中(实际上你可能会持有一个听众列表):
public class SomeObject {
private EventListener lst;
public void setListener (EventListener lst) {
this.lst = lst;
}
public void somethingHappened () {
lst.fireEvent(new Event("Something Happened"));
}
}
回答by mleczey
You can use PropertyChangeSupportwith PropertyChangeListeneror use Observer pattern.
您可以将PropertyChangeSupport与PropertyChangeListener 一起使用或使用观察者模式。
回答by Aleksander Gralak
First of all you need some source of events, so you can attache listener to it. If you need custom listener then you need also implement the custom source.
首先,您需要一些事件源,因此您可以将侦听器附加到它。如果您需要自定义侦听器,则还需要实现自定义源。
In Java you can find existing sources and listener
interfaces. As you mentioned GUI is usually based on events. If you are into 3D then rendering engines deliver appropriate API (eg. collision detection
), file system hooks, properties change listeners (Android
).
在 Java 中,您可以找到现有的源代码和listener
接口。正如您提到的,GUI 通常基于事件。如果您喜欢 3D,那么渲染引擎会提供适当的 API(例如collision detection
)、文件系统挂钩、属性更改侦听器(Android
)。
It depends what are your needs. For most usages there should be already a library that delivers you appropriate API.
这取决于您的需求是什么。对于大多数用途,应该已经有一个库可以为您提供适当的 API。
While implementing your own solution then for application wide event handling the Event Bus might be a good choice. My preferred implementation is in Guava library: http://code.google.com/p/guava-libraries/wiki/EventBusExplained
在实现您自己的解决方案时,对于应用程序范围的事件处理,事件总线可能是一个不错的选择。我的首选实现是在番石榴库中:http: //code.google.com/p/guava-libraries/wiki/EventBusExplained
回答by HericDenis
You can implement kind of listeners in Java extending Observableclass for the objects you want to observe, and on the listeners, you implement Observer.
您可以在 Java 中为要观察的对象扩展Observable类来实现某种侦听器,并在侦听器上实现Observer。
回答by duffymo
You don't need frameworks or Observer class. It's all built into the Java Beans spec since version 1.0 in 1995. It was supposed to be Java's answer to VB properties.
您不需要框架或 Observer 类。自 1995 年的 1.0 版以来,所有这些都内置在 Java Beans 规范中。它应该是 Java 对 VB 属性的回答。
Here's a tutorial:
这是一个教程:
http://docs.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html
http://docs.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html