到达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:");