Call a method when application closes

流过昼夜 提交于 2019-12-18 09:34:16

问题


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?


回答1:


  • 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.

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);
    }
}



回答2:


Use the window events on your JFrame, there you have the Methods you might need (windowclosed();) for example. it´s the WindowListener

edit :

you can say

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

but your Windowlistener still works if you push the X (close button)

there you override the method windowClosing, with this code

public void windowClosing(WindowEvent e) {
    int i = JOptionPane.showConfirmDialog(TestFrame.this, "do you really want to close?","test",JOptionPane.YES_NO_OPTION);
    if(i == 0) {
        System.exit(0);
    }
}

this will do the work



来源:https://stackoverflow.com/questions/13800621/call-a-method-when-application-closes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!