2013-06-19 80 views
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對話框顯示在應用程序的後臺?我是編程新手,我仍然無法解決這個錯誤。JOption對話框在後臺顯示

+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

或者您可以只請求重點... – 2013-06-19 17:05:21

0

使用的null位置this ..

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

在main(String [])'中不能使用'this'! –

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