微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java:如何取消应用程序退出

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

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

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

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

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

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

有任何想法吗 ?

解决方法

更换
mainframe.setDefaultCloSEOperation (JFrame.disPOSE_ON_CLOSE);

mainframe.setDefaultCloSEOperation (JFrame.DO_nothing_ON_CLOSE);

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

原文地址:https://www.jb51.cc/java/128012.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐