Java Documentlistener检查我的文本字段是否为空

Java Documentlistener检查我的文本字段是否为空,java,swing,actionlistener,jtextfield,documentlistener,Java,Swing,Actionlistener,Jtextfield,Documentlistener,我有一个默认禁用的按钮。我希望在我的文本字段不再为空时启用该按钮。我试图使用documentlistener,但它给出了以下错误: method addDocumentListener in interface javax.swing.text.Document cannot be applied to given types; required: javax.swing.event.DocumentListener found: <anonymous DocumentListen

我有一个默认禁用的按钮。我希望在我的文本字段不再为空时启用该按钮。我试图使用
documentlistener
,但它给出了以下错误:

method addDocumentListener in interface javax.swing.text.Document cannot be applied to given types;
  required: javax.swing.event.DocumentListener
  found: <anonymous DocumentListener>
  reason: actual argument <anonymous DocumentListener> cannot be converted to javax.swing.event.DocumentListener by method invocation conversion

原因:无法通过方法调用转换将实际参数转换为javax.swing.event.DocumentListener。

您是否(可能错误地)在其他地方声明了自己的
类DocumentListener
?i、 如果你改变了,会发生什么

tekstveld.getDocument().addDocumentListener(new DocumentListener(){

?


(尽管我同意上面的评论,在action performed方法中设置文档侦听器似乎是个坏主意;这应该在创建表单时设置。)

您是否也在添加文档侦听器的同一字段中添加了操作侦听器?向我们展示更多的代码,或者编写一个小的测试程序来解释这个问题。我不太了解你上面发布的堆栈跟踪。我无法复制这个。转换
DocumentListener
实现时似乎遇到了问题。古怪的也许可以尝试将
DocumentListener
放在一个私有类或整个其他文件中?可能重复:我也想到了这一点,但情况并非如此,因为它将在编译时捕获,而不是在运行时。@williamorrison对我来说,错误看起来像编译时消息:-)@williamorrison运行时消息将是堆栈跟踪
tekstveld.getDocument().addDocumentListener(new DocumentListener(){
tekstveld.getDocument().addDocumentListener(new javax.swing.event.DocumentListener(){