我像这样创建 DocumentListeners:
MyDocumentListener dl = new MyDocumentListener();
tab.getDocument().addDocumentListener( dl );
MyDocumentListener 是一个提供事件处理的类。但是,我想删除 DocumentListener,但我不知道如何引用我创建的 MyDocumentListener 对象。
最佳答案
简短的回答是:您需要将您创建的 MyDocumentListener 对象存储在可以访问的地方。我不知道你的代码的结构,所以我不能说保存它的最佳位置。如果您想立即删除刚刚添加的文档监听器,只需执行以下操作:
MyDocumentListener dl = new MyDocumentListener();
tab.getDocument().addDocumentListener( dl );
tab.getDocument().removeDocumentListener( dl );
...但这可能不是您想要的(为什么要删除刚刚添加的文档监听器?)因此,请考虑使用有关软件结构的更多详细信息编辑问题,这样也许回答者可以提出位置存储您创建的 DocumentListener。
关于java - 如何引用通过类创建的 DocumentListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29174362/