繁体   English   中英

应用程序关闭时调用方法

[英]Call a method when application closes

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

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

我该如何实现?

  • 调用JFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE)
  • WindowListener添加到框架。
  • 重写侦听器的适当方法以调用您的关闭方法,然后将框架设置为不可见并进行处理。

例如

对话框,检查框架退出

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

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

这会做的工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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