当用户点击“X”时,我想更改下面的代码以显示“是”或“否”选项,但我担心我的java新手技能还没有延伸到它。有什么建议吗?我想尽可能保持下面的代码完整无缺,以便了解需要采取哪些不同的方式以供将来参考。
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class WindowExit extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
JOptionPane.showMessageDialog( null, "Are you sure you want to close?" );
System.exit(0);
}
}
答案 0 :(得分:11)
使用showConfirmDialog,如下所示:
int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to close?", "Close?", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION)
{
System.exit(0);
}
答案 1 :(得分:6)
查看docs。您可以将JOptionPane.YES_NO_OPTION
作为参数传递。
答案 2 :(得分:2)
public static int showConfirmDialog(Component parentComponent,
Object message,
String title,
int optionType)
optionType
JOptionPane.YES_NO_OPTION