JOptionPane.showMessageDialog无效AnnotationName

时间:2015-01-05 12:38:16

标签: java

以下一些java课程初学者水平,进入了这个问题:

Multiple markers at this line
    - Syntax error on token "showMessageDialog", invalid 
     AnnotationName
    - Syntax error on token "(", { expected after this token
    - Syntax error on tokens, ConstructorHeaderName expected instead

,该行是:

JOptionPane.showMessageDialog(null, "The answer is " +sum, "The Title", JOptionPane.INFORMATION_MESSAGE);

Java Docs说这个方法接受4个参数,所以我发送4个参数。我也导入了所需的库:

import javax.swing.JOptionPane;

JOptionPane.showInputDialog工作正常,但不是JOptionPane.showMessageDialog。我错过了什么? 谢谢。


完整代码

package java_practice;
import javax.swing.JOptionPane;

public class GraphicalUserInterface {
    String first_number = JOptionPane.showInputDialog("Enter first number");
    String second_number = JOptionPane.showInputDialog("Enter second number");

    int num1 = Integer.parseInt(first_number);
    int num2 = Integer.parseInt(second_number);
    int sum = num1 + num2;

    JOptionPane.showMessageDialog(null, "The answer is " +sum, "The Title", JOptionPane.INFORMATION_MESSAGE);
}

1 个答案:

答案 0 :(得分:5)

您错过了方法声明。您的代码位于类的主体内部,这会使编译器/ IDE混淆,例如,关于无效注释的错误等。

需要在某种形式,块或静态块的方法中。

public class GraphicalUserInterface {
    public static void main(String [] args) {  //  <=========

        String first_number = JOptionPane.showInputDialog("Enter first number");
        String second_number = JOptionPane.showInputDialog("Enter second number");

        int num1 = Integer.parseInt(first_number);
        int num2 = Integer.parseInt(second_number);
        int sum = num1 + num2;

        JOptionPane.showMessageDialog(null, "The answer is " +sum, "The Title", JOptionPane.INFORMATION_MESSAGE);

   } //  <=========
}