1

I have an application in Java. I'd like to specify the "exit code" of my console application. How can I apply an exit code for the 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);
1
  • Why create JTextFields in actionPerformed(ActionEvent actionEvent)? Commented Apr 20, 2015 at 13:43

4 Answers 4

2

From what I understand you want to terminate with an exit code

System.exit(status);
Sign up to request clarification or add additional context in comments.

3 Comments

hi, i have add system,exit(0) under panel.add(registerButton). but my java application not showing..
Not showing what? Is it not showing the exit code when it terminates?
@Kei Please read what System.exist() does, then you will know what happens if you add that line in your "normal" program flow.
2

you can use System.exit(0)

public static void exit(int status) Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination. This method calls the exit method in class Runtime. This method never returns normally.

The call System.exit(n) is effectively equivalent to the call:

Runtime.getRuntime().exit(n) Parameters: status - exit status. Throws: SecurityException - if a security manager exists and its checkExit method doesn't allow exit with the specified status. See Also: Runtime.exit(int)

Comments

2

To exit a java program use:

System.exit(exit_code);

The exit code should be 0 if it is a normal exit and any other number means an abnormal termination. You can read more information about it here: https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#exit(int)

Comments

0

You should use System.exit(int StatusCode). This will exit your Java program with the specified exit code, eg. System.exit(0) will exit your program with status code 0.

From the docs:

Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.