应用程序关闭时调用方法

时间:2012-12-10 12:06:49

标签: java swing servlets awt

在我的Swing聊天应用程序中,我有一个注销按钮,用于注销用户,它工作正常。现在,我需要在关闭Swing应用程序窗口时注销用户。

我在使用JavaScript关闭浏览器时在Web应用程序中这样做了,但现在我需要在Swing应用程序中执行此操作。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:6)

  • 致电JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
  • 在框架中添加WindowListener
  • 覆盖侦听器的相应方法以调用close方法,然后将该帧设置为不可见并将其丢弃。

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

答案 1 :(得分:1)

使用JFrame上的窗口事件,您可以获得可能需要的方法(windowclosed();)。它是WindowListener

编辑:

你可以说

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

但是如果你按下X(关闭按钮)

,你的Windowlistener仍然有效

你用这个代码重写方法windowClosing

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

这将完成工作