如何使用退出代码退出程序:Java



我有一个Java应用程序。我想指定控制台应用程序的"退出代码"。如何为registerButton应用退出代码?

final JTextField passwordText = new JTextField(20);
passwordText.setBounds(10, 120, 160, 25);
panel.add(passwordText);
JButton loginButton = new JButton("OK");
loginButton.setBounds(10, 150, 80, 25);
panel.add(loginButton);
JButton registerButton = new JButton("CANCEL");
registerButton.setBounds(180, 150, 80, 25);
panel.add(registerButton);
loginButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        int i=Integer.parseInt(passwordText.getText());
        if(1<=i &&i<4) {
            JTextField xField1 = new JTextField(5);
            JTextField xField2 = new JTextField(5);
            JTextField xField3 = new JTextField(5);
            JTextField xField4 = new JTextField(5);
您可以使用

System.exit(0)

公共静态无效退出(整数状态) 终止当前正在运行的 Java 虚拟机。参数用作状态代码;由 约定,非零状态代码表示异常终止。这 方法调用类运行时中的退出方法。此方法从不 正常返回。

调用 System.exit(n) 实际上等同于调用:

Runtime.getRuntime().exit(n) 参数:status - exit status。 抛出:安全异常 - 如果存在安全管理器并且其 checkExit 方法不允许以指定状态退出。看 另外:Runtime.exit(int)

要退出 Java 程序,请使用:

System.exit(exit_code);

如果是正常退出,则应0退出代码,任何其他数字都表示异常终止。 您可以在此处阅读有关它的更多信息:https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#exit(int)

我了解,您想以退出代码终止

System.exit(status);

你应该使用 System.exit(int StatusCode) .这将使用指定的退出代码退出您的 Java 程序,例如。 System.exit(0) 将退出程序,状态代码为 0

从文档中:

终止当前正在运行的 Java 虚拟机。参数用作状态代码;按照惯例,非零状态代码表示异常终止。

最新更新