2
我有一个应用程序。我需要做到这一点,当用户点击关闭按钮时,会有一个确认(就像javascript确认对话框),'你确定'?我在Netbeans中使用Swing可视化编辑器,并且在关闭窗口时使用了一个侦听器。但我无法做到。任何帮助?Java制作确认退出
我有一个应用程序。我需要做到这一点,当用户点击关闭按钮时,会有一个确认(就像javascript确认对话框),'你确定'?我在Netbeans中使用Swing可视化编辑器,并且在关闭窗口时使用了一个侦听器。但我无法做到。任何帮助?Java制作确认退出
试试这个代码:
jj4.addActionListener(new ActionListener() // jj4-button's reference to implement exit
{
public void actionPerformed(ActionEvent ae)
{
if(!jtextarea.getText().equals("") && !jtextarea.getText().equals(fileContent))
{
if(fileName == null)
{
//this method have 3 options (1 = YES, 2 = NO, 3 = Cancel)
option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
if(option == 0)
{
//to save the text into file
saveAs();
System.exit(0);
}
if(option == 1)
{
//to exit the program without saving
System.exit(0);
}
}
else
{
option = JOptionPane.showConfirmDialog(null,"Do you want to save the changes ??");
if(option == 0)
{
save();
System.exit(0);
}
if(option == 1)
{
System.exit(0);
}
}
}
else
{
System.exit(0);
}
}
});
不要使用神奇的数字! JOptionPane定义了可变参数来检查是,否和取消。 – camickr
请先搜索论坛,因为这已被要求,并回答了一个极大倍。例如,[how-can-a-swing-windowlistener-veto-jframe-closing](http://stackoverflow.com/questions/3777146/how-can-a-swing-windowlistener-veto-jframe-closing)。你也说你正在使用WindowListner(正确的方法),但“我无法做到” - 但是不会告诉我们你尝试过什么。请这样做,否则我们不能告诉你你做错了什么。 –
糟糕,“WindowListener” –
@HovercraftFullOfEels我当时找不到正确的关键字。感谢您的建议... – Dewsworld