Java:如何取消应用程序退出 [英] Java: How to cancel application exit

查看:160
本文介绍了Java:如何取消应用程序退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个程序中,我希望在用户尝试退出应用程序时显示一个Dialog。然后,用户必须选择保存程序的某些状态,而不是保存或取消退出操作。

On one of my programs, I want a Dialog to appear when the user attempts to exit the application. The user must then choose to save some state of the program, not to save or to cancel the exit operation.

我写这篇文章是为了首先找到解决方案,然后实现它:

I wrote this in an attempt to find a solution first and ony then implement it:

import javax.swing.*;
import java.awt.Dimension;
import java.awt.event.*;

class WL implements WindowListener
{
    private boolean statussaved;
    private JFrame tframe;

    WL (JFrame frame)
    {
        statussaved = false;
        tframe = frame;
    }

    @Override public void windowActivated (WindowEvent w) { }
    @Override public void windowClosed (WindowEvent w) { }
    @Override public void windowDeactivated (WindowEvent w) { }
    @Override public void windowDeiconified (WindowEvent w) { }
    @Override public void windowIconified (WindowEvent w) { }
    @Override public void windowOpened (WindowEvent w) { }

    @Override public void windowClosing (WindowEvent w)
    {
        if (statussaved)
        {
            return;
        }

        final JDialog diag = new JDialog (tframe, "Save Progress", true);
        diag.setPreferredSize (new Dimension (500, 100));
        diag.setResizable (false);
        diag.setDefaultCloseOperation (JDialog.DISPOSE_ON_CLOSE);

        JPanel notifypanel = new JPanel ();
        notifypanel.add (new JLabel ("Do you want to save the current status ?"));

        JButton yesbutton = new JButton ("Yes");
        JButton nobutton = new JButton ("No");
        JButton cancelbutton = new JButton ("Cancel");

        yesbutton.addActionListener (new ActionListener ()
        {
            @Override public void actionPerformed (ActionEvent a)
            {
                //SAVE THE STATUS

                System.out.println ("Saving status...");
                statussaved = true;

                diag.dispose ();
                tframe.dispose ();
            }
        });

        nobutton.addActionListener (new ActionListener ()
        {
            @Override public void actionPerformed (ActionEvent a)
            {
                //just exit/close the application

                diag.dispose ();
                tframe.dispose ();
            }
        });

        cancelbutton.addActionListener (new ActionListener ()
        {
            @Override public void actionPerformed (ActionEvent a)
            {
                //DON'T EXIT !!!

                diag.dispose ();
            }
        });

        notifypanel.add (yesbutton);
        notifypanel.add (nobutton);
        notifypanel.add (cancelbutton);

        diag.setContentPane (notifypanel);

        diag.pack ();
        diag.setVisible (true);
    }
}

public class SaveTest
{
    public static void main (String[] args)
    {
        SwingUtilities.invokeLater (new Runnable ()
        {
            @Override public void run ()
            {
                JFrame frame = new JFrame ("Save Test");
                frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
                frame.addWindowListener (new WL (frame));

                JLabel lab = new JLabel ("just some information");

                frame.setPreferredSize (new Dimension (400, 300));
                frame.setResizable (false);
                frame.add (lab);
                frame.pack ();
                frame.setVisible (true);
            }
        });
    }
}

它编译并运行没有任何变化,所以你可以测试它。

It compiles and runs without any change, so you can test it.

是和否选项按预期工作,但我完全不知道在 ActionListener中写什么/c $ c>取消按钮。我想要的是,当用户点击取消按钮时,对话框消失,但主窗口仍然可见(即程序继续运行)。

The "Yes" and "No" choices work as expected, but I have absolutely no idea what to write in the ActionListener of the "Cancel" button. What I want is, when the user clicks the "Cancel" button, the dialog dissapears, but the main window remains visible (i.e. the program keeps running).

现在,因为所有这些都是在 windowClosing 方法中实现的,所以很明显发送了某种处理信号以销毁 JFrame 。这意味着在当前设计中可能无法做到这一点。我不介意重新组织/重新设计所有这些以使其工作。它只是......我不知道如何。

Now, since all this is implemented in the windowClosing method, it's kind of clear that some sort of dispose signal was sent in order to destroy the JFrame. This means that there is probably no way this can be done in the current design. I don't mind reorganizing/redesigning all this to make it work. It's just... I don't know how.

任何想法?

推荐答案

替换

mainframe.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);

with

mainframe.setDefaultCloseOperation (JFrame.DO_NOTHING_ON_CLOSE);

如果用户取消关闭 - 什么都不做。如果同意 - 手动调用 dispose()

If user cancels closing - do nothing. If agrees - call dispose() manually.

这篇关于Java:如何取消应用程序退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆