Ask user if wants to quit via JOptionPane

早过忘川 提交于 2019-12-25 11:47:22

问题


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

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?


回答1:


just do this:

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);



回答2:


You can try something like this:

    import javax.swing.*;
    import java.awt.event.*;
    public class MyFrame extends JFrame
    {
        public MyFrame()
        {
            setTitle("Close Me");
            setSize(200,200);
            setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            addWindowListener(new WindowAdapter()
            {
                    @Override
                    public void windowClosing(WindowEvent evt)
                    {
                        Object[] options = {"Quit, My Computing Fellow", "No, I want to Work more"};

                        int answer = JOptionPane.showOptionDialog(MyFrame.this, "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); 
                        }
                    }
            });
        }
        public static void main(String st[])
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    MyFrame mf = new MyFrame();
                    mf.setVisible(true);
                }
            });
        }
    }

As a side note I would suggest you to stick with java naming conventions. For example the variable name should never start with capital letter, class name should always start with capital letter .. And many more. Have a look at here Code Conventions for the Java Programming Language




回答3:


try something like this:

{
...
yourFrame.setDefaultCloseOperation(close());
...
}

private int close() {
if(yourCondition)
    return JFrame.DO_NOTHING_ON_CLOSE;
else 
    return JFrame.EXIT_ON_CLOSE;
}



回答4:


I have a Real blunder

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

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!



来源:https://stackoverflow.com/questions/16367242/ask-user-if-wants-to-quit-via-joptionpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!