是否可以编写自己的对象发出是ActionEvent? [英] Is it possible to write your own objects that give out ActionEvents?

查看:290
本文介绍了是否可以编写自己的对象发出是ActionEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java教程在网上看了,他们似乎都关心捕获由已写入其他组件给出了是ActionEvent。是否有可能编写,有自己的一套触发然后可以通过已注册为侦听其他类被抓是ActionEvent标准,自己的对象?

I've looked at the java tutorials online and they all seem concerned with catching ActionEvents given out by other components that are already written. Is it possible to write your own objects that have there own set of criteria that trigger actionEvents that can then be caught by other classes that have registered as listeners?

因此​​,例如:如果我想这是数羊时,100只羊已经数到了登记的侦听器的所有卧铺对象发出一个ActionEvent对象。

So for example: If I wanted an object that was counting sheep to send out an actionEvent when 100 sheep had been counted to all the sleeper objects that had registered as listeners.

有没有办法做到这一点是没有任何在线教程?

Is there a way to do this are there any tutorials online?

任何帮助是极大AP preciated。

Any help is greatly appreciated.

推荐答案

是的,这是pretty简单,一旦有人向您展示如何创建自己的听众。

Yes, it's pretty straightforward, once someone shows you how to create your own listeners.

首先,您要创建自己的EventObject。下面是我的一个项目为例。

First, you create your own EventObject. Here's an example from one of my projects.

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,但您可以创建任何你想要的方法。

There's nothing special about this class, other than it extends EventObject. Your constructor is defined by EventObject, but you can create any methods you want.

第二,你定义一个EventListener接口。

Second, you define an EventListener interface.

public interface EventListener {

    public void handleEvent(InmatePhotoEventObject eo);

}

您将使用您创建在eventObject。您可以使用任何你想要的方法的名称或名称。这是为将被写为到听者的响应code中的接口

You would use the EventObject you created. You can use any method name or names that you want. This is the interface for the code that will be written as a response to the listener.

三,你写一个ListenerHandler。下面是从同一个项目我的。

Third, you write a ListenerHandler. Here's mine from the same project.

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的。

The main method in this class shows you how you use a ListenerHandler. The rest of the methods in the class are standard. You would use your own EventObject and EventListener.

这篇关于是否可以编写自己的对象发出是ActionEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆