关于this关于 WeakListeners
的文档我发现代码被剪掉了:
private void registerTo(Source source) {
listener = new Listener();
source.addChangeListener(WeakListeners.change (listener, source));
}
private class Listener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
doSomething();
}
他们正在写的那个源对象是什么?我们在谷歌上找不到任何东西。
最佳答案
我检查了您链接的文档。 再往下说:
It is itself strongly referenced from the implementation of the source (e.g. from its EventListenerList)
给你一个可以在那里使用的类的例子。 确实在文档中
WeakListeners.change (listener, source)
源是一个对象,因此它可以是您用作事件源的任何内容。 如果您继续阅读该文档,您会看到:
It tries to unregister itself from the source. This is why it needs the reference to the source for the registration. The unregistration is done using reflection, usually looking up the method remove of the source and calling it.
还有
This may fail if the source don't have the expected remove* method and/or if you provide wrong reference to source. In that case the weak listener instance will stay in memory and registered by the source, while the listener and observer will be freed.
所以我想您将需要使用带有某种删除方法的接口(interface)。
我浏览了 Javadoc 索引,发现这个 ( MenuBar ) 作为我认为您正在寻找的示例。
希望这有帮助。
干杯!
关于java - 实现弱监听器时,监听器添加到什么源类型? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58911352/