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

问题描述:

我想知道如何在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.

查看使用监听器的任何类的源代码。实际上它非常简单:

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


  • 为你的听众创建一个界面,例如: MyListener

  • 维护一份 MyListener

  • 在侦听器应该侦听的每个事件上,遍历列表并使用某些事件参数调用适当的方法

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

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