我有一個Java程序。當我運行這個程序時,它會給我一個GUI,就像我附上的那樣。JoptionPane ShowConfirmDialog
當我想關閉它時,它會提示出一個確認對話框。如果我按下「是」按鈕,它將使用System.exit()
退出程序。
public static void main(String args[])
{
ButtonTest app = new ButtonTest();
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{
String message = " Really Quit ? ";
String title = "Quit";
int reply = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
);
}
如果我不想退出程序,該怎麼辦? System.continued()
?
刪除'else {}'部分。 –
但是當我點擊沒有,整個程序不見了,但我可以看到在命令提示符下,它仍然工作。 – Eric
嘗試添加'app.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);'。 –