我收到一条错误消息:
no suitable method found for showMessageDialog(<anonymous Runnable>,String,String,int)
当我尝试使用 JOptionPane.show...
方法。这是为什么?
private void connectivityChecker() {
Runnable r = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
}catch(Exception exc) {
System.out.println("Thread Interrupted !");
}
boolean isConnected = Internet.isConnected();
if(!isConnected) {
JOptionPane.showMessageDialog(this, "You have lost connectivity to the server", "Connection Error", JOptionPane.ERROR_MESSAGE);
}
}
};
new Thread(r,"Connectivity Checker - UserGUI").start();
}
最佳答案
当您引用 this
时,它指向内部类,而不是您正在考虑的外部类。
尝试将该点告诉外部类
,而不是匿名内部类
。
JOptionPane.showMessageDialog(OuterClassName.this, <--------
"You have lost connectivity to the server",
"Connection Error", JOptionPane.ERROR_MESSAGE);
关于java - 当我在匿名类中尝试时,为什么会收到此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19240380/