سؤال

first sorry for my bad english.

Hi, i'm trying to use a confirmDialog with a YES_NO_OPTION. what i want is that when i close a frame a confimDialog will be displayed asking me if want to close.

if i press yes everyThing most be closed, if i press no the confirmDialog will disapear

but the problem is even if i press no button the frame close this is my code:

final JFrame frame = new JFrame("2D Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1600,600);
frame.setResizable(false);

    private void continuerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_continuerButtonActionPerformed
        int level=getlevel();
        System.out.println(niveau);
        if(niveau == 1)
        {
            this.dispose();
            frame.add(new Board());
            frame.setVisible(true);
            frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) 
            {
                doExitOption();
            }
            });
        }
    }

and this is doExitOption methode:

 public void doExitOption()
    {
        int option=JOptionPane.showConfirmDialog(null, "do you want to quit the game",    "Warnning",JOptionPane.YES_NO_OPTION);
            if(option == JOptionPane.YES_OPTION)
            {
                frame.dispose();
            }
    }
هل كانت مفيدة؟

المحلول

See Closing an Application. It can manager the default close operation for you.

نصائح أخرى

You need to change the JFrame's default close operation so that your call to dispose is the only call being made to dispose the window:

frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

Change Default closing of JFrame to DO_NOTHING_ON_CLOSE .

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top