消息对话框错误
问题描述:
我得到这个错误:消息对话框错误
cannot find symbol
symbol: method showMessageDialog(<anonymous javax.swing.AbstractAction>,java.lang.String,java.lang.String,int)
有人能帮助我吗? 感谢
exitAction = new
AbstractAction("Esci") {
public void actionPerformed(ActionEvent e) {
if (rcStatus ==1) {
JOptionPane.showMessageDialog(this,
"Thread running. Choose STOP before
exit",
"Error", JOptionPane.ERROR_MESSAGE);
}
else {
System.exit(0);}
}
};
exitAction.putValue(Action.NAME,
"Exit");
exitAction.putValue(Action.SHORT_DESCRIPTION,"Close");
答
有一个在JOptionPane
与签名没有方法。你确定你通过的this
是AbstractAction
,它不是Component
。对于showMessageDialog()
,these are your options。
我想你想JOptionPane.showMessageDialog(Component parentComponent, Object message, String title, int messageType)
。如果你没有合适的父组件传递,传递null
而不是this
:
JOptionPane.showMessageDialog(null, "Thread running. Choose STOP before exit", "Error", JOptionPane.ERROR_MESSAGE);
答
在你的方法调用,this
指的是要创建匿名类(扩展AbstractAction)。
如果此代码是一个组件内,如:
public class MyComponent extends JComponent {
...
(your code)
那么您可以在方法调用更改为:
JOptionPane.showMessageDialog(MyComponent.this, "Thread running", ...
MyComponent.this
指外部类对象。
它是private void createActions(){......}的一部分我应该重写整个代码吗?谢谢 – max 2011-01-12 15:39:36