Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

java - Call a method when application closes

In my Swing chat application I have a logout button which is used to logout the user and it works fine. Now I need to logout the user when I close the Swing application window.

I did this in web application when closing browser using JavaScript, but now I need to do this in Swing application.

How can I achieve this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • Call JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
  • Add a WindowListener to the frame.
  • Override the appropriate method of the listener to call your closing method, then set the frame invisible and dispose of it.

E.G.

Dialog, checking for frame exit

import java.awt.*;
import java.awt.event.*;
import java.net.URI;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

class CheckExit {

    public static void doSomething() {
        try {
            // do something irritating..
            URI uri = new URI(
                    "http://stackoverflow.com/users/418556/andrew-thompson");
            Desktop.getDesktop().browse(uri);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                // the GUI as seen by the user (without frame)
                JPanel gui = new JPanel(new BorderLayout());
                gui.setPreferredSize(new Dimension(400, 100));
                gui.setBackground(Color.WHITE);

                final JFrame f = new JFrame("Demo");
                f.setLocationByPlatform(true);
                f.add(gui);

                // Tell the frame to 'do nothing'.
                f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

                WindowListener listener = new WindowAdapter() {

                    @Override
                    public void windowClosing(WindowEvent we) {
                        int result = JOptionPane.showConfirmDialog(
                                f, "Close the application");
                        if (result==JOptionPane.OK_OPTION) {
                            doSomething();
                            f.setVisible(false);
                            f.dispose();
                        }
                    }
                };
                f.addWindowListener(listener);

                // ensures the frame is the minimum size it needs to be
                // in order display the components within it
                f.pack();
                // should be done last, to avoid flickering, moving,
                // resizing artifacts.
                f.setVisible(true);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...