如何在java中创建自定义监听器?

发布于 2024-11-28 03:56:16 字数 84 浏览 0 评论 0原文

我想知道如何在java中设置我们自己的监听器。例如,我有一个将数字从1增加到100的函数。我想在值达到50时设置一个监听器。我该怎么做?请建议我任何教程。

I want to know about how to set our own Listeners in java.For example I have a function that increments number from 1 to 100. i want to set a listener when the value reaches 50. How can i do that? Pls suggest me any tutorial.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

淡淡の花香 2024-12-05 03:56:16

https://stackoverflow.com/a/6270150/3675925

您可能想查看观察者模式

可以帮助您入门的示例代码:

import java.util.*;

// An interface to be implemented by everyone interested in "Hello" events
interface HelloListener {
    void someoneSaidHello();
}

// Someone who says "Hello"
class Initiater {
    private List<HelloListener> listeners = new ArrayList<HelloListener>();

    public void addListener(HelloListener toAdd) {
        listeners.add(toAdd);
    }

    public void sayHello() {
        System.out.println("Hello!!");

        // Notify everybody that may be interested.
        for (HelloListener hl : listeners)
            hl.someoneSaidHello();
    }
}

// Someone interested in "Hello" events
class Responder implements HelloListener {
    @Override
    public void someoneSaidHello() {
        System.out.println("Hello there...");
    }
}

以下是一些

class Test {
    public static void main(String[] args) {
        Initiater initiater = new Initiater();
        Responder responder = new Responder();

        initiater.addListener(responder);

        initiater.sayHello();  // Prints "Hello!!!" and "Hello there..."
    }
}

https://stackoverflow.com/a/6270150/3675925

You probably want to look into the observer pattern.

Here's some sample code to get you started:

import java.util.*;

// An interface to be implemented by everyone interested in "Hello" events
interface HelloListener {
    void someoneSaidHello();
}

// Someone who says "Hello"
class Initiater {
    private List<HelloListener> listeners = new ArrayList<HelloListener>();

    public void addListener(HelloListener toAdd) {
        listeners.add(toAdd);
    }

    public void sayHello() {
        System.out.println("Hello!!");

        // Notify everybody that may be interested.
        for (HelloListener hl : listeners)
            hl.someoneSaidHello();
    }
}

// Someone interested in "Hello" events
class Responder implements HelloListener {
    @Override
    public void someoneSaidHello() {
        System.out.println("Hello there...");
    }
}

 

class Test {
    public static void main(String[] args) {
        Initiater initiater = new Initiater();
        Responder responder = new Responder();

        initiater.addListener(responder);

        initiater.sayHello();  // Prints "Hello!!!" and "Hello there..."
    }
}
白昼 2024-12-05 03:56:16

查看使用侦听器的任何类的源代码。事实上,这很简单:

  • 为您的侦听器创建一个接口,例如 MyListener
  • 在侦听器应该侦听的每个事件上维护一个 MyListener 列表
  • ,迭代该列表并调用带有一些事件参数的适当方法

至于观察者模式以及一些Java代码,请查看wikipedia

Have a look at the source of any class that uses listeners. In fact it's quite easy:

  • create an interface for your listener, e.g. MyListener
  • maintain a list of MyListener
  • upon each event that the listeners should listen to, iterate over the list and call the appropriate method with some event parameter(s)

As for the observer pattern along with some Java code have a look at wikipedia.

爱*していゐ 2024-12-05 03:56:16

没有内置机制允许您将侦听器附加到所有变量。您想要观看的对象需要为其提供支持。例如,它可以成为 Observable 并向其观察者触发 onChange 事件(您还必须确保其被跟踪)。

There is no built-in mechanism that would allow you to attach listeners to all variables. The object you want to watch needs to provide the support for that by itself. For example it could become Observable and fire off onChange events to its Observers (which you also have to ensure are being tracked).

瑶笙 2024-12-05 03:56:16

我建议您针对您的用例使用 EventBus 。它有一个很好的API设计并且易于使用。请查看他们的入门部分,了解其工作原理。

I would recommend using EventBus for your use case. It has a nice API design and is easy to use. Have a look at their Getting Started section to see how it works.

爱给你人给你 2024-12-05 03:56:16

您可以使用 Signals 库。它看起来像这样:

interface FiftySignal{
    void on50();
}

class FiftyMaker{
    FiftySignal fiftySignal = Signals.signal(FiftySignal.class).dispatcher;

    void doIt(){
        for(int i = 0; i < 100; i++){
            if(i == 50){
                fiftySignal.on50(); // dispatching the event
            }
        }   
    }
}

class Boo{
    Signal<FiftySignal> fiftySignal = Signals.signal(FiftySignal.class);


    void bar(){
         fiftySignal.addListener(()-> System.out.println("It's 50!")); // listener
    }
}

免责声明:我是 Signals 的作者。

You can use the Signals library for it. It will look like that:

interface FiftySignal{
    void on50();
}

class FiftyMaker{
    FiftySignal fiftySignal = Signals.signal(FiftySignal.class).dispatcher;

    void doIt(){
        for(int i = 0; i < 100; i++){
            if(i == 50){
                fiftySignal.on50(); // dispatching the event
            }
        }   
    }
}

class Boo{
    Signal<FiftySignal> fiftySignal = Signals.signal(FiftySignal.class);


    void bar(){
         fiftySignal.addListener(()-> System.out.println("It's 50!")); // listener
    }
}

Disclaimer: I am the author of Signals.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文