Java确认退出 [英] Java making confirming exit

查看:180
本文介绍了Java确认退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序。我需要这样做,当用户点击关闭按钮时,确认将会出现(就像javascript确认对话框),你确定吗?我在Netbeans中使用Swing可视化编辑器,我在关闭窗口时使用一个监听器。但我不能做到。任何帮助?

解决方案

尝试此代码:

  jj4.addActionListener(new ActionListener()// jj4-button的引用实现exit 
{
public void actionPerformed(ActionEvent ae)
{
if !jtextarea.getText()。equals()&!jtextarea.getText()。equals(fileContent))
{
if(fileName == null)
{
//这个方法有3个选项(1 = YES,2 = NO,3 =取消)
option = JOptionPane.showConfirmDialog(null,你要保存更改吗);
if(option == 0)
{
//将文本保存到文件
saveAs();
System.exit(0);
}
if(option == 1)
{
// to exi程序不保存
System.exit(0);
}
}
else
{
option = JOptionPane.showConfirmDialog(null,你要保存更改吗);
if(option == 0)
{
save();
System.exit(0);
}
if(option == 1)
{
System.exit(0);
}
}
}
else
{
System.exit(0);
}
}
});


I have an application. I need to make this, when user click the close button, a confirmation will come ( just like javascript confirm dialog ) , 'Are you sure'? I'm using Swing visual editor in Netbeans and I'm using a listener on window closing. But I can't make it. Any help?

解决方案

Try this code:

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);
            }
        }
    });

这篇关于Java确认退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆