Java JOptionPane.showMessage对话框无法打开

时间:2015-09-06 20:14:01

标签: java netbeans joptionpane messagebox

我可以使用以下行打开一个消息框:JOptionPane.showMessageDialog(null, "Hello " + name); 但是当我在它之前添加一些代码时:

Scanner keyboard = new Scanner(System.in);
     System.out.println("What is your name?");
            name = keyboard.nextLine();

我再也无法弹出对话框了。第一个print语句正常工作,但在输入名称后,程序只是继续运行而什么都不做。

这是我使用Netbeans的完整代码,谢谢!

package chapter2practice;

//Here I import classes
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
 *
 * @author Berley2001
 */

public class Main {

    public static void main(String[] args) {
        String name;
        //Here I create an object from the Scanner class
        Scanner keyboard = new Scanner(System.in);

        System.out.println("What is your name?");
        name = keyboard.nextLine();

        //Here I add a message from the JOptionPane class
        JOptionPane.showMessageDialog(null, "Hello " + name);
        System.exit(0);
    }

}

0 个答案:

没有答案