简体   繁体   中英

Confirm Dialog not clicking cancel

I was making a Tic Tac Toe game and have a menu option exit. When you click on it, a confirm dialog pops up with OK and Cancel. When I click ok, it stops running. But when I click cancel nothing happens and the dialog is still there. How can I include that in code.

else if (e.getActionCommand().equals("Exit")) {
    int option = JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Exit Game", JOptionPane.OK_CANCEL_OPTION);

    if (option == JOptionPane.OK_OPTION)
        System.exit(0);
    else if (option == JOptionPane.CANCEL_OPTION) {
        window.add(board, BorderLayout.CENTER);
        window.setJMenuBar(jmb);                
    }
}

try this code

 else if (e.getActionCommand().equals("Exit")) {
    int option = JOptionPane.showConfirmDialog(null, "Do you want to exit?", "Exit Game", JOptionPane.OK_CANCEL_OPTION);

    if (option == JOptionPane.OK_OPTION)}
        System.exit(0);
}
   else if (option == JOptionPane.CANCEL_OPTION) {
     window.setDefaultCloseOperation(**JFrame(or)your panel**.DO_NOTHING_ON_CLOSE);
        window.add(board, BorderLayout.CENTER);
        window.setJMenuBar(jmb);                
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM