JOption对话框显示在后台

时间:2013-06-19 16:59:28

标签: java swing joptionpane

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对话框会在应用程序的后台显示?我是编程新手,但仍无法解决此错误。

3 个答案:

答案 0 :(得分: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);
        }
}

答案 1 :(得分:0)

this的位置使用 null ..

JOptionPane.showMessageDialog(this,"your answer is" + " " + (num1 + num2));

答案 2 :(得分: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);