简体   繁体   English

用Java创建自定义监听器

[英]Creating Custom Listeners In Java

I was wondering how it would be possible to create my own listener for something, and to be able to use it just like any other listener, ex. 我想知道如何为某些东西创建我自己的监听器,并且能够像任何其他监听器一样使用它,例如。

public interface Listener {

    public void onListen(Event e);

}

And then this in my class 然后在我的班上

public class test implements Listener {

    Object.add(new Listener() { 

        @Override
        public void onListen(Event e) {

        }
    });
}

I guess what i'm really asking also is where do I define how to check if something happens pertaining to the listener that I create? 我想我真正要问的是,我在哪里定义如何检查是否发生了与我创建的监听器有关的事情?

If any of this doesn't make sense, please tell me and I will clarify it. 如果其中任何一个没有意义,请告诉我,我会澄清它。

If at best this makes no sense to you, i'll try this. 如果充其量这对你没有意义,我会尝试这个。 How would I make a listener to check if the selected option in a combobox has changed to a different option. 如何让侦听器检查组合框中的选定选项是否已更改为其他选项。 I don't actually need a combobox listener though, that was just an example. 我实际上并不需要一个组合框监听器,这只是一个例子。

Here is a short example: 这是一个简短的例子:

    private static class Position {
    }

    private static class Ball {
        private ArrayList<FootballObserver> observers = new ArrayList<FootballObserver>();
        private Position ballPosition = new Position();

        public void registerObserver(FootballObserver observer) {
            observers.add(observer);
        }

        public void notifyListeners() {
            for(FootballObserver observer : observers) {
                observer.notify(ballPosition);
            }
        }

        public void doSomethingWithFootballPosition() {
            //bounce etc
            notifyListeners();
        }
    }

    private static interface FootballObserver {
        void notify(Position ball);
    }

    private static class Player implements FootballObserver {
        public void notify(Position ball) {
            System.out.println("received new ball position");
        }
    }

    public static void main(String... args) {
        FootballObserver player1 = new Player();
        Ball football = new Ball();
        football.registerObserver(player1);
        football.doSomethingWithFootballPosition();
    }

There is no trouble creating the listeners that you need, but you only can apply it to classes that expect it (so you cannot do (new Object()).addMyListener ) 创建所需的侦听器没有问题,但是只能将它应用于期望它的类(所以你不能这样做(new Object()).addMyListener

You will need to add to the classes where you want to use the listener an addMyListener(MyListener myListener) method. 您需要向要使用侦听器的类添加addMyListener(MyListener myListener)方法。 It just stores the listeners that you pass to it in a list for later user. 它只是将您传递给它的侦听器存储在列表中供以后用户使用。 It would be a good idea creating an interface with the addMyListener method (and a fireMyListeners() ) method too. 使用addMyListener方法(以及fireMyListeners() )方法创建接口也是一个好主意。

Of course, you must also provide the code to call the fireMyListeners() method, too. 当然,您还必须提供代码来调用fireMyListeners()方法。 Then the fireMyListeners() will just loop through the listeners and call theirs notification method. 然后fireMyListeners()将循环遍历监听器并调用他们的通知方法。

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

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