首先对不起我的英语不好。
嗨,我正在尝试使用带有 YES_NO_OPTION 的 confirmDialog。我想要的是,当我关闭一个框架时,会显示一个 confimDialog,询问我是否要关闭。
如果我按是,每件事都被关闭,如果我按否,confirmDialog 将消失
但问题是即使我没有按任何按钮框架关闭这是我的代码:
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();
}
});
}
}
这是doExitOption
方法:
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();
}
}