Call a method when application closes

前端 未结 2 1720
长情又很酷
长情又很酷 2020-12-22 02:44

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 wind

相关标签:
2条回答
  • 2020-12-22 03:33

    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

    0 讨论(0)
  • 2020-12-22 03:44
    • 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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题