我有一个申请。我需要这样做,当用户点击关闭按钮时,会出现确认信息(就像javascript确认对话框一样),“你确定吗?”我在Netbeans中使用Swing可视化编辑器,我在关闭窗口时使用了一个监听器。但我做不到。有什么帮助吗?
答案 0 :(得分:6)
试试这段代码:
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);
}
}
});