Java 弹出框不工作

Java Popup Box not Working

我是 Java 的新手,但对编程并不陌生。在我尝试熟悉语法的过程中,我一直在慢慢开发一个构建在复杂性中的顺序程序。我试图添加一个弹出框,但是我注意到它在我使用扫描仪后不起作用,但是如果我在扫描仪之前有一个弹出框,它就会起作用!有人可以解释一下吗?我把开头的弹出框注释掉了。当它被注释掉时,代码到达了我打算显示的弹出框,显示"Complete!",但是弹出框没有出现,代码也没有继续。但是,如果我取消注释掉最初的空白弹出框,第二个弹出框会正常工作,程序将继续正常运行。

import java.util.*;
import javax.swing.JOptionPane;
public class Tutorial1 
{


static final double PINUM = 3.141592654;        // Declaring a constant

public static void main(String[] args) 
{
    //JOptionPane.showMessageDialog(null,"");

    int myInt = 4, i;            // Declares an integer
    double myDouble;            // Declares Double
    int[] myArray = new int[50]; // Declares Array

    Scanner in = new Scanner(System.in);
    System.out.println("\n\nWelcome! Please enter your name!");
    String userName = in.nextLine();

    System.out.println("Nice to meet you, " + userName + ".\n");

    System.out.println("\nThe largest float this machine can create is = " + Float.MAX_VALUE);
    System.out.println("\nThe largest double this machien can create is = " + Double.MAX_VALUE);

    System.out.println("\nThis is a test Program for learning Java \nCreated by " + userName 
            + "\n7/29/15\n\n"); // the + carries this line from the last

    myInt++;
    System.out.println("Value : " + myInt);

    myDouble = 4.77;
    myDouble = (myInt-1)/myDouble;

    System.out.println("Value : " + myDouble + "\n");

    for(myInt = 5; myInt >= 0; myInt--)
    {
        System.out.println("Loop Value : " + myInt);
    }

    System.out.println("\n\nTesting Popup box...");
    JOptionPane.showMessageDialog(null,"Complete!");

    System.out.println("\n\nTest Done");        
}

}

关于为什么会发生这种情况以及如何解决它,您有什么想法吗?我希望学习和熟悉 Java,所以尽可能详细。谢谢!

请试试这个..

import java.util.*;
import javax.swing.JOptionPane;
public class Tutorial1  extends JFrame
 {


  static final double PINUM = 3.141592654;        // Declaring a constant

public static void main(String[] args) 
 {
  JFrame jf=new JFrame();
//JOptionPane.showMessageDialog(null,"");

int myInt = 4, i;            // Declares an integer
double myDouble;            // Declares Double
int[] myArray = new int[50]; // Declares Array

Scanner in = new Scanner(System.in);
System.out.println("\n\nWelcome! Please enter your name!");
String userName = in.nextLine();

System.out.println("Nice to meet you, " + userName + ".\n");

System.out.println("\nThe largest float this machine can create is = " +            Float.MAX_VALUE);
System.out.println("\nThe largest double this machien can create is = " +   Double.MAX_VALUE);

System.out.println("\nThis is a test Program for learning Java \nCreated by  " + userName 
        + "\n7/29/15\n\n"); // the + carries this line from the last

myInt++;
System.out.println("Value : " + myInt);

myDouble = 4.77;
myDouble = (myInt-1)/myDouble;

System.out.println("Value : " + myDouble + "\n");

for(myInt = 5; myInt >= 0; myInt--)
{
    System.out.println("Loop Value : " + myInt);
}

System.out.println("\n\nTesting Popup box...");
JOptionPane.showMessageDialog(jf,"Complete!");

System.out.println("\n\nTest Done");        
}
}

JOptionPane.showMessageDialog(jf,"Complete!");听说您将 JFrame 对象作为消息对话框显示的第一个参数传递。