如何停止关闭Java应用程序 [英] How can I stop the closing of a Java application

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

问题描述

我有一个Java桌面应用程序,我希望当用户选择退出"以获取一个弹出窗口时,询问他是否要继续关闭该应用程序.我知道如何显示窗口并读取用户的响应,但我需要知道的是如何停止关闭应用程序(例如System.close().cancel()).

I have a Java Desktop application and I would like when the user selects Exit to get a pop-up window that asks him if he wants to proceed with closing the application or not. I know how to make the window come up and read the user's response but what I need to know is how I can stop the application from closing (something like System.close().cancel()).

有可能吗?

推荐答案

是的.

调用setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)后,添加 WindowListener WindowAdapter并在windowClosing(WindowEvent)方法中,弹出 .

After calling setDefaultCloseOperation(DO_NOTHING_ON_CLOSE), add a WindowListener or WindowAdapter and in the windowClosing(WindowEvent) method, pop a JOptionPane.

int result = JOptionPane.showConfirmDialog(frame, "Exit the application?");
if (result==JOptionPane.OK_OPTION) {
    System.exit(0);     
}

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

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