为什么我会收到“找不到适合匿名的方法”错误?
Runnable r = new Runnable() {
@Override
public void run() {
if(varx) {
new displayFullScreen().setVisible(true);
} else {
javax.swing.JOptionPane.showMessageDialog(this, "dfv"); // this statement gives an error
}
}
};
new Thread(r,"full_screen_display").start();
标记行中的错误显示 “没有找到适合匿名的方法 (
”
当我直接编写 时为什么会这样javax.swing._CLASS_
?
Runnable r = new Runnable() {
@Override
public void run() {
if(varx) {
new displayFullScreen().setVisible(true);
} else {
javax.swing.JOptionPane.showMessageDialog(this, "dfv"); // this statement gives an error
}
}
};
new Thread(r,"full_screen_display").start();
The error in the marked line says "No suitable method found for anonymous (<java.lang.Runnable>,java.lang.String)
"
Why does it so when i have directly written javax.swing._CLASS_
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于该行中的
this
引用了您创建的匿名Runnable
实例,而不是它周围的类。您需要更明确地说明其中的this
的含义。如果封闭类名为
Foo
,并且是一个 swingComponent
,则应编写:请参阅 嵌套类 文档以获取更多信息。
The problem is that
this
in that line refers to the anonymousRunnable
instance you've created, not the class that surrounds it. You'll need to be more explicit about whatthis
you mean in there.If the enclosing class is named
Foo
, and is a swingComponent
, you should write:See the Nested Classes docs for more information.
原因是
javax.swing.JOptionPane.showMessageDialog
需要一个Component
作为第一个参数,但您传入的是this
,它是一个可运行
(匿名)。The reason is
javax.swing.JOptionPane.showMessageDialog
expects aComponent
as the first argument, but you're passing inthis
, which is aRunnable
(anonymous).JOptionPane.showMessageDialog
文档 说:javax.swing.JOptionPane.showMessageDialog(this, "dfv");
将不起作用,因为this
是一个Runnable
,它不继承自组件
。使用这个代替:
JOptionPane.showMessageDialog
documentation says:javax.swing.JOptionPane.showMessageDialog(this, "dfv");
will not work asthis
is aRunnable
which does not inherit fromComponent
.Use this instead: