0
import java.util.Scanner;
import javax.swing.JOptionPane;

public class calculator {

public static void main(String[] args)
{
    int num1;
    int num2;

    String operation;

    Scanner input = new Scanner(System.in);

    System.out.println("please enter the first number");
    num1 = input.nextInt();

    System.out.println("please enter the second number");
    num2 = input.nextInt();


    System.out.println("Please enter operation");
    operation = input.next();

    if (operation.equals ("+"))
    {
        JOptionPane.showMessageDialog(null,"your answer is" + " " + (num1 + num2));
    }
    if  (operation.equals ("-"))
    {
        JOptionPane.showMessageDialog(null, "your answer is " + " " + (num1 - num2));
    }

    if (operation.equals ("/"))
    {
        JOptionPane.showMessageDialog(null,"your answer is" + " " + " " + (num1 / num2));
    }
    if (operation.equals ("*"))
    {
        JOptionPane.showMessageDialog(null,"your answer is" + " " + " " + (num1 * num2));
    }
}
}

您好,为什么应用程序的后台会显示 JOption 对话框?我是编程新手,但仍然无法解决此错误。

4

3 回答 3

0

在..this的位置使用null

JOptionPane.showMessageDialog(this,"your answer is" + " " + (num1 + num2));
于 2013-06-19T17:20:23.033 回答
0

尝试这个:

import java.util.Scanner;
import javax.swing.JOptionPane;

public class calculator {

public static void main(String[] args)
{
    int num1;
    int num2;

    String operation;

    Scanner input = new Scanner(System.in);

    System.out.println("please enter the first number");
    num1 = input.nextInt();

    System.out.println("please enter the second number");
    num2 = input.nextInt();


    System.out.println("Please enter operation");
    operation = input.next();

        JOptionPane optionPane = new JOptionPane();

        if (operation.equals ("+"))
        {
            JDialog dialog = optionPane.createDialog("your answer is" + " " + (num1 + num2));
            dialog.setAlwaysOnTop(alwaysOnTop);
            dialog.setVisible(true);

        }
        if  (operation.equals ("-"))
        {
            JDialog dialog = optionPane.createDialog("your answer is " + " " + (num1 - num2));
            dialog.setAlwaysOnTop(alwaysOnTop);
            dialog.setVisible(true);
        }

        if (operation.equals ("/"))
        {
             JDialog dialog = optionPane.createDialog("your answer is" + " " + " " + (num1 / num2));
            dialog.setAlwaysOnTop(alwaysOnTop);
            dialog.setVisible(true);

        }
        if (operation.equals ("*"))
        {
            JDialog dialog = optionPane.createDialog("your answer is" + " " + " " + (num1 * num2));
            dialog.setAlwaysOnTop(alwaysOnTop);
            dialog.setVisible(true);
        }
}
于 2013-06-19T17:02:03.160 回答
0

尝试这个

 UIManager UI=new UIManager();
 UI.put("OptionPane.background",new ColorUIResource(0,250,0)); 
    //      (or)  
 UI.put("OptionPane.background", Color.green);

 UI.put("Panel.background",new ColorUIResource(255,0,0));
   //       (or)
 UI.put("Panel.background", Color.red);

 JOptionPane.showMessageDialog(null,"Warning Message for change the color of the ","Forum", JOptionPane.WARNING_MESSAGE);
于 2015-06-11T07:17:56.923 回答