我在关闭特定JFrame之前使用windowClosing
进行确认。
在关闭之前,我得到一个确认对话框,但问题是即使我单击否按钮也会关闭。有什么帮助吗?
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we)
{
String ObjButtons[] = {"Yes","No"};
int PromptResult = JOptionPane.showOptionDialog(null,
"Are you sure you want to exit?", "Online Examination System",
JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null,
ObjButtons,ObjButtons[1]);
if(PromptResult==0)
{
System.exit(0);
}
}
});
答案 0 :(得分:12)
您的JFrame默认关闭操作设置为什么?您需要确保将其设置为:jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
答案 1 :(得分:10)
试试这个
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we)
{
String ObjButtons[] = {"Yes","No"};
int PromptResult = JOptionPane.showOptionDialog(null,"Are you sure you want to exit?","Online Examination System",JOptionPane.DEFAULT_OPTION,JOptionPane.WARNING_MESSAGE,null,ObjButtons,ObjButtons[1]);
if(PromptResult==JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
});
答案 2 :(得分:2)
如果JFrame设置为DISPOSE_ON_CLOSE
,则可以将其默认关闭选项设置为EXIT_ON_CLOSE
。在这种情况下,它将解决您的查询,如: -
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
答案 3 :(得分:2)
将其置于帧初始化
上frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);