简体   繁体   中英

How to use Exit button in Swing Application?

I am new in Java Form Application development using Swing. What is the suitable code for exit button that will exit the application?

For example: In C# (.NET Framework) we use Application.Exit(); as the code for exit what button. What is the equivalent code in Java to exit an application?

jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Now for a bit of explanation. The closest equivalent to as seen in the question is..

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

But that kills the entire JRE irrespective of other non-daemon threads that are running. If they are running, they should be shut down or cleaned up explicitly.

If there are no other non-daemon threads running, DISPOSE_ON_CLOSE will dispose the current JFrame and end the virtual machine.

@AndrewThompson is right about JFrame.EXIT_ON_CLOSE but you can take steps to avoid just killing stuff.

JFrame.EXIT_ON_CLOSE is just fine, if you handle cleanup in an overridden JFrame.processWindowEvent(WindowEvent) checking for WindowEvent.WINDOW_CLOSING events.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.Timer;

public class ExitJFrame extends JFrame {

    public ExitJFrame() {
        setLayout(new BorderLayout());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("Exit");
        add(button);

        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ExitJFrame.this.processWindowEvent(
                        new WindowEvent(
                                ExitJFrame.this, WindowEvent.WINDOW_CLOSING));
            }
        });

        setSize(200, 200);
        setLocationRelativeTo(null);
    }

    @Override
    protected void processWindowEvent(WindowEvent e) {
        // more powerful as a WindowListener, since you can consume the event 
        // if you so please - asking the user if she really wants to exit for
        // example, do not delegate to super if consuming.
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            doCleanup();
            super.processWindowEvent(e); 
        } else {        
            super.processWindowEvent(e); 
        }
    }

    private void doCleanup() {
        final JDialog dialog = new JDialog(this, true);
        dialog.setLayout(new BorderLayout());
        dialog.setUndecorated(true);
        JProgressBar progress = new JProgressBar();
        dialog.add(progress);
        dialog.add(new JLabel("Waiting for non-daemon threads to exit gracefully..."), BorderLayout.NORTH);
        progress.setIndeterminate(true);

        Timer timer = new Timer(2000, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
                dialog.dispose();
            }
        });
        timer.setRepeats(false);
        timer.start();
        dialog.pack();
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }

    public static void main (String[] args) {
        new ExitJFrame().setVisible(true);
    }

}

If you want to create a button which exits the application when the user clicks it, try this:

JButton exit = new JButton("exit");

ActionListener al = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.exit(0);
    }
};

exit.addActionListener(al);

Now when you press on that button the application will exit.

If you're talking about a JButton that you want to put into your form, then you want to invoke JFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) in your constructor for the form, and then your button handler can invoke

dispose()

to close the form.

Very simple answer is as @Andrew Thompson said.

jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

OR

jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

and if u want to make it in other way by code. then u can put this code anywhere. in events.

System.exit(0);

hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM