Java 使用 joptionpane 退出程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24091730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:06:37  来源:igfitidea点击:

Using joptionpane to exit the program

javaswingjoptionpane

提问by Mian Asbat Ahmad

I have a gui java program that count the number of files (if any) in a folder and store their names in an array for processing. In the case when no files are available to process, I want to display a joptionpane with the message " No files are available to process. Please click the exit button to exit the system ".

我有一个 gui java 程序,它计算文件夹中文件(如果有)的数量,并将它们的名称存储在一个数组中以供处理。在没有文件可处理的情况下,我想显示一个带有消息“没有文件可处理。请单击退出按钮退出系统”的 joptionpane。

How can I exit the system when the user click the button of the joptionpane? Something like

当用户单击 joptionpane 的按钮时如何退出系统?就像是

if (array.length == 0){
    JOptionPane.show .......
    {
        System.exit(0);
    }
}

采纳答案by David Yee

You can store the return value of the JOptionPaneas such:

您可以这样存储返回值JOptionPane

int result = JOptionPane.showConfirmDialog(window,
        "Are you sure you want to quit?",
        "Confirm Quit", JOptionPane.YES_NO_CANCEL_OPTION);
if (result == JOptionPane.YES_OPTION) System.exit(0);

回答by Mian Asbat Ahmad

Thanks David for your help. The following is what I was looking for.

感谢大卫的帮助。以下是我正在寻找的内容。

        if (filesToCompileArray.length == 0){

        int result = JOptionPane.showConfirmDialog(null,
                "No failure found in the SUT and no files are generated to process further",
                "Confirm Quit", JOptionPane.DEFAULT_OPTION);
        if (result == 0) System.exit(0);

    }