消息对话框错误

时间:2011-01-12 15:14:33

标签: java swing

我收到此错误:

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");

2 个答案:

答案 0 :(得分:1)

JOptionPane中没有带有该签名的方法。您确定传入的thisAbstractAction,而不是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);

答案 1 :(得分:0)

在方法调用中,this引用您正在创建的匿名类(扩展了AbstractAction)。

如果此代码位于Component内部,例如:

public class MyComponent extends JComponent {
...
(your code)

然后您可以将方法调用更改为:

JOptionPane.showMessageDialog(MyComponent.this, "Thread running", ...

MyComponent.this引用外部类对象。