【问题标题】:Call a method when application closes应用程序关闭时调用方法
【发布时间】:2012-12-12 08:25:04
【问题描述】:

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

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

我怎样才能做到这一点?

【问题讨论】:

    标签: java swing servlets awt


    【解决方案1】:
    • 致电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);
        }
    }
    

    【讨论】:

    • 感谢您的支持...它有效,但当我尝试关闭窗口时还需要一个确认框...
    • 感谢您的回复...工作正常,但如果我按取消按钮,我的应用程序已关闭...如果我按取消按钮,我不需要关闭应用程序窗口。 ..
    【解决方案2】:

    在您的 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);
        }
    }
    

    这样就可以了

    【讨论】:

    • 感谢您的支持...它有效,但当我尝试关闭窗口时还需要一个确认框...
    • 感谢您的回复...但是如果我按取消或否按钮我不需要关闭应用程序应用程序...请帮助我...
    • TestFrame 是我的 JFrame 类。你可以用你的 JFrame 来改变它。我是我确认消息的回复。因此,如果您在出现问题时单击“是”,您的应用程序将关闭,否则它什么也不做
    • 写下classname.this.或干脆写下this.
    猜你喜欢
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-18
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多