本文介绍了询问用户是否要通过JOptionPane退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码来确认用户单击JFrame的红色十字关闭按钮(右上角)时是否要退出

I'm using this code to confirm the user whether wants to quit or not when he CLICKS THE RED CROSS CLOSE BUTTON OF JFrame (right upper corner)

 Object[] options = {"Quit, My Computing Fellow", "No, I want to Work more"};

int Answer = JOptionPane.showOptionDialog(null, "What would you like to do? ","Quit:Continue", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
    null, options,options[1]);
    if(Answer == JOptionPane.YES_OPTION){

        System.exit(0);
    }
    else if (Answer == JOptionPane.CANCEL_OPTION) {
        return;
    }

但是问题是,如果用户单击CANCEL_OPTION,则框架完全关闭,但是我希望用户仍然打开框架,而不允许框架关闭.指导我,如果我正在做大事或其他事情?

but the problem is if the user clicks CANCEL_OPTION the Frame Closes at all, but i want the user to still open the Frame and not allow the Frame to close. Guide me If I'm doing the blunder or something else?

推荐答案

我有一个严重失误

   Object[] options = {"Quit, My Computing Fellow", "No, I want to Work more"};

   int Answer = JOptionPane.showOptionDialog(null, "What would you like to do?","Quit:Continue", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
null, options,options[1]);
if(Answer == JOptionPane.YES_OPTION){

    System.exit(0);
}
else if (Answer == JOptionPane.CANCEL_OPTION) {
    return;
}

很明显,我有两个选项,即YES_NO_OPTION,并且我打电话给CANCEL_OPTION真是个大错,所以else-if应该更改为:

It was clear that I have two Options i.e YES_NO_OPTION and I was calling the CANCEL_OPTION that was a real blunder so, the else-if should be changed to:

else if (Answer == JOptionPane.NO_OPTION) {
    this.setDefaultCloseOperation(myclassreference.DO_NOTHING_ON_CLOSE);
}

在此之后;它的宾果游戏!我做完了!

after this; its bingo !!! I've done!

这篇关于询问用户是否要通过JOptionPane退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 15:40
查看更多