简体   繁体   English

扩展ActionEvent吗? 没有自定义侦听器的自定义事件?

[英]Extending ActionEvent? A custom event, without a custom listener?

I've 2 Objects I'm concerned with at the moment: a controller and a class creating an event. 目前,我有2个对象涉及到:一个控制器和一个创建事件的类。

I've got it all working with an ActionListener variable in the source class, set by an anonymous ActionListener object and method from the controller. 我已经在源类中使用ActionListener变量进行了所有工作,该变量由控制器的匿名ActionListener对象和方法设置。

But I need access to data from the source class back in the controller. 但是我需要从控制器中的源类访问数据。 I'm capable of extending EventListener and using a custom EventObject, but that seems a bit overkill for one bit of data. 我能够扩展EventListener并使用自定义EventObject,但这似乎对于一点点数据来说有点过大。

Is there an easy way to just extend ActionEvent and create an extra variable that I can access in the Listener's actionPerformed() method? 有没有简单的方法来扩展ActionEvent并创建一个我可以在侦听器的actionPerformed()方法中访问的额外变量? ActionEvent's constructor is just confusing me. ActionEvent的构造函数让我感到困惑。

public class NotesEvent extends ActionEvent{
    public NotesEvent(Object source, int id, String command){
        super(source, id, command);
    }
}

I don't know what to pass for the 'identifier' when instantiating the event. 我不知道实例化事件时传递给“标识符”的内容。

Maybe I've missed something simple - I've only ever really learned to do this the long way round with custom classes. 也许我错过了一些简单的事情-我只是真正学会了使用自定义类进行长期的操作。

Any help would be appreciated 任何帮助,将不胜感激

I was making the mistake of not downcasting the custom event in the controller, so I thought that ActionListener wouldn't play nice with a custom event - but it does seem to (which is what I initially thought) 我犯了一个错误,就是没有在控制器中向下转换自定义事件,所以我认为ActionListener不能与自定义事件一起很好地使用-但这确实(我最初的想法)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class Target {
    ActionListener listener;
    int data = 1;

    public Target(){
        JButton b = new JButton("Press Me");

        b.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                CustomEvent ce = new CustomEvent(data, Target.this, 0, "Command");
                if(listener != null) listener.actionPerformed(ce);
            }
        });
    }

    public void setListener(ActionListener listener){
        listener = listener;
    }  
}

class CustomEvent extends ActionEvent{
    int data;
    CustomEvent(int data, Object source, int id, String command){
        super(source, id, command);
        this.data = data;
    }

    public int getData(){
        return data;
    }
}

The controller class: 控制器类:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class Controller {
    Target t = new Target();

    public static void main(String[] args){
        Controller c = new Controller();
    }

    public Controller(){
        t.setListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
               //here was my problem 
               System.out.println(((CustomEvent)e).getID());
            }
        });
    }
}

I'm still not entirely sure about the data I should be passing to the ActionEvent constructor though. 我仍然不确定我应该传递给ActionEvent构造函数的数据。

Hope the above makes clear what I was trying to achieve (I know it's not actually complete in terms of the UI appearing.) 希望以上内容可以清楚说明我要实现的目标(我知道就UI而言,它实际上还没有完成。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM