我查看了在线的 java 教程,它们似乎都关心捕获其他已经编写的组件发出的 ActionEvents。是否可以编写自己的对象,这些对象具有自己的一组触发 actionEvents 的条件,然后可以被其他注册为侦听器的类捕获?
例如:如果我想要一个正在计数绵羊的对象在所有已注册为侦听器的睡眠对象计数 100 只时发送一个 actionEvent。
有没有办法做到这一点,网上有教程吗?
任何帮助是极大的赞赏。
我查看了在线的 java 教程,它们似乎都关心捕获其他已经编写的组件发出的 ActionEvents。是否可以编写自己的对象,这些对象具有自己的一组触发 actionEvents 的条件,然后可以被其他注册为侦听器的类捕获?
例如:如果我想要一个正在计数绵羊的对象在所有已注册为侦听器的睡眠对象计数 100 只时发送一个 actionEvent。
有没有办法做到这一点,网上有教程吗?
任何帮助是极大的赞赏。
是的,这很简单,只要有人向您展示如何创建自己的听众。
首先,您创建自己的 EventObject。这是我的一个项目的一个例子。
import gov.bop.rabid.datahandler.bean.InmateDataBean;
import java.util.EventObject;
public class InmatePhotoEventObject extends EventObject {
private static final long serialVersionUID = 1L;
protected InmateDataBean inmate;
public InmatePhotoEventObject(Object source) {
super(source);
}
public InmateDataBean getInmate() {
return inmate;
}
public void setInmate(InmateDataBean inmate) {
this.inmate = inmate;
}
}
这个类没有什么特别的,除了它扩展了EventObject。您的构造函数由 EventObject 定义,但您可以创建任何您想要的方法。
其次,定义一个 EventListener 接口。
public interface EventListener {
public void handleEvent(InmatePhotoEventObject eo);
}
您将使用您创建的 EventObject。您可以使用任何您想要的方法名称或名称。这是将作为对侦听器的响应而编写的代码的接口。
第三,您编写一个 ListenerHandler。这是我来自同一个项目的。
import gov.bop.rabid.datahandler.bean.InmateDataBean;
import gov.bop.rabid.datahandler.main.EventListener;
import gov.bop.rabid.datahandler.main.InmatePhotoEventListener;
import gov.bop.rabid.datahandler.main.InmatePhotoEventObject;
import java.util.ArrayList;
import java.util.List;
public class InmatePhotoListenerHandler {
protected List<EventListener> listeners;
public InmatePhotoListenerHandler() {
listeners = new ArrayList<EventListener>();
}
public void addListener(EventListener listener) {
listeners.add(listener);
}
public void removeListener(EventListener listener) {
for (int i = listeners.size() - 1; i >= 0; i--) {
EventListener instance = listeners.get(i);
if (instance.equals(listener)) {
listeners.remove(i);
}
}
}
public void fireEvent(final InmatePhotoEventObject eo,
final InmateDataBean inmate) {
for (int i = 0; i < listeners.size(); i++) {
final EventListener instance = listeners.get(i);
Runnable runnable = new Runnable() {
public void run() {
eo.setInmate(inmate);
instance.handleEvent(eo);
}
};
new Thread(runnable).start();
}
}
public static void main(String[] args) {
System.out.println("This line goes in your DataHandlerMain class "
+ "constructor.");
InmatePhotoListenerHandler handler = new InmatePhotoListenerHandler();
System.out.println("I need you to put the commented method in "
+ "DataHandlerMain so I can use the handler instance.");
// public InmatePhotoListenerHandler getInmatePhotoListenerHandler() {
// return handler;
// }
System.out.println("This line goes in the GUI code.");
handler.addListener(new InmatePhotoEventListener());
System.out.println("Later, when you've received the response from "
+ "the web service...");
InmateDataBean inmate = new InmateDataBean();
inmate.setIntKey(23);
handler.fireEvent(new InmatePhotoEventObject(handler), inmate);
}
}
此类中的主要方法向您展示了如何使用 ListenerHandler。类中的其余方法是标准的。您将使用自己的 EventObject 和 EventListener。
是的。
我建议您查看 ActionEvent 和 EventListenerList 的 java API 文档。
我还建议您阅读有关侦听器(也称为观察者)模式的信息。