到达main方法的末尾时程序不会终止 [英] Program doesn't terminate when reaching the end of the main method

查看:322
本文介绍了到达main方法的末尾时程序不会终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,在启动该程序之前,需要用户的输入.

I have a program where I ask for an input from the user before I launch it.

public static void main(String args[]) 
{
    String database = JOptionPane.showInputDialog(new JFrame(), "Enter a DB:");
    if(database!=null && database.foundInDB()) {
        SPVerification spv = new SPVerification();
        spv.setVisible(true);
    }
    //System.exit(1);  Without it the program doesn't terminate although it's the end
    //                 of the main function.                  
}

如果用户输入未找到的数据库,则不应执行该程序.

If the user enters a database that's not found, the program shouldn't be executed.

当我输入错误的数据库名称时,if语句下面的代码不会执行,因此我到达了main方法的末尾,但是程序没有终止,但是如果我在if之后添加了system.exit(1) if语句,程序终止.尽管我已经到达main的末尾,为什么我仍然需要打电话给System.exit(1)?

When I enter a wrong DB name, the code below if statement doesn't execute, so I reach the end of the main method, but the program doesn't terminate, but if I add system.exit(1) after the if statement, the program terminates. Why do I need to call System.exit(1) although I've reached the end of main?

推荐答案

您已经创建了一个新的JFrame,默认情况下它不会关闭,因为没有任何东西可以触发要处置的窗户,例如WindowEvent .由于这似乎是基于非UI的应用程序,因此您可以简单地使用:

You have created a new JFrame which, by default, will not close as there is nothing to trigger the window to be disposed such as a WindowEvent. As this appears to be a non-UI based application, you could simply use:

JOptionPane.showInputDialog(null, "Enter a DB:");

这篇关于到达main方法的末尾时程序不会终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆