Ask user if wants to quit via JOptionPane

2019-09-10 02:12发布

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?

4条回答
forever°为你锁心
2楼-- · 2019-09-10 02:28

just do this:

setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
查看更多
戒情不戒烟
3楼-- · 2019-09-10 02:34

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!

查看更多
Anthone
4楼-- · 2019-09-10 02:44

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

查看更多
趁早两清
5楼-- · 2019-09-10 02:46

try something like this:

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

private int close() {
if(yourCondition)
    return JFrame.DO_NOTHING_ON_CLOSE;
else 
    return JFrame.EXIT_ON_CLOSE;
}
查看更多
登录 后发表回答