Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java catch块中的Joptionpane消息对话框_Java - Fatal编程技术网

Java catch块中的Joptionpane消息对话框

Java catch块中的Joptionpane消息对话框,java,Java,我正在使用Swing将目录和文件从一台windows服务器复制到另一台windows服务器,效果很好。我希望在复制时windows服务器意外停机时弹出Joption Messagedialog,因此在catch块中提供了它,但在服务器停机时它从不显示弹出窗口(复制时我手动重新启动windows服务器,但看不到弹出窗口)。有人能帮忙吗?这是密码 try { textarea.append("Copying " + sourceFile.getAbsolutePath() +

我正在使用Swing将目录和文件从一台windows服务器复制到另一台windows服务器,效果很好。我希望在复制时windows服务器意外停机时弹出Joption Messagedialog,因此在catch块中提供了它,但在服务器停机时它从不显示弹出窗口(复制时我手动重新启动windows服务器,但看不到弹出窗口)。有人能帮忙吗?这是密码

try {
    textarea.append("Copying " + sourceFile.getAbsolutePath()
        + "   to " + targetFile.getAbsolutePath());
    is = new BufferedInputStream(new FileInputStream(sourceFile));
    bos = new BufferedOutputStream(new FileOutputStream(targetFile));

    long fileBytes = sourceFile.length();
    long soFar = 0;

    int theByte;

    while ((theByte = bis.read()) != -1) {
        bos.write(theByte);

        setProgress((int) (copiedBytes++ * 100 / totalBytes));
        publish((int) (soFar++ * 100 / fileBytes));
    }

    bis.close();
    bos.close();
    publish(100);
    textarea.append(" Done!\n");
} catch (Exception excep) {
    task.cancel(true);
    bos.flush();
    bis.close();
    bos.close();
    jf2 = new JFrame();
    jf2.setSize(401, 401);
    jf2.setDefaultCloseOperation(jf2.EXIT_ON_CLOSE);
    JOptionPane.showMessageDialog(jf2,
        "The Server is not accessible or it may be down because of Network Issue",
        "ERROR", JOptionPane.ERROR_MESSAGE);
} finally {
    if (bis != null) {
        bis.close();
    }
    if (bos != null) {
        bos.close();
    }
}

首先,我不喜欢所有的手动资源管理,所以我会改变这种方式,使用Java 7来为您进行管理,这允许删除finally块和
close()
flush()
(顺便说一句,close调用flush,这样您就不需要两者)

其次,我不知道该消息框声明是否有效,
JOptionPane.showMessageDialog()的Javadoc
说第一个参数应该是
Component parentComponent
,但是你声明了一个新的不可见的JFrame,因此如果你发布的代码在JFrame类中,请传入
this
。总之,我要试一试:

try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile)))
{
    textarea.append("Copying " + sourceFile.getAbsolutePath() +
                    " to " + targetFile.getAbsolutePath());
    long fileBytes = sourceFile.length();
    long soFar = 0;
    int theByte;
    while((theByte = bis.read()) != -1)
    {
        bos.write(theByte);
        setProgress((int) (copiedBytes++ * 100 / totalBytes));
        publish((int) (soFar++ * 100 / fileBytes));
    }
    publish(100);
    textarea.append(" Done!\n");
}
catch(Exception excep)
{
    task.cancel(true);
    JOptionPane.showMessageDialog(this, "The Server is not accessible or it may be down because of Network Issue", "ERROR", JOptionPane.ERROR_MESSAGE);
}

你的试捕有点不对劲

在发生异常时,您尝试在
finally
块内关闭流

Finally保证会被调用,所以您可以使用它来关闭蒸汽,从而节省一些代码

try {
    textarea.append("Copying " + sourceFile.getAbsolutePath()
                    + "   to " + targetFile.getAbsolutePath());
    is = new BufferedInputStream(new FileInputStream(sourceFile));
    bos = new BufferedOutputStream(new FileOutputStream(targetFile));

    long fileBytes = sourceFile.length();
    long soFar = 0;

    int theByte;

    while ((theByte = bis.read()) != -1) {
        bos.write(theByte);

        setProgress((int) (copiedBytes++ * 100 / totalBytes));
        publish((int) (soFar++ * 100 / fileBytes));
    }

    // Not required, finally will take care of it...
    //bis.close();
    //bos.close();
    publish(100);
    // !! THIS IS VERY, VERY NAUGHTY !!
    textarea.append(" Done!\n");
} catch (Exception excep) {

    JOptionPane.showMessageDialog(null, "The Server is not accessible or it may be down because of Network Issue", "ERROR", JOptionPane.ERROR_MESSAGE);
    task.cancel(true);

} finally {

    try {
        // techniqually, this gets taken care of when you close the stream,
        // but I tend not to trust it either...
        bos.flush();
    } catch (Exception e) {
    }

    try {
        bis.close();
    } catch (Exception e) {
    }
    try {
        bos.close();
    } catch (Exception e) {
    }

}
您的代码似乎使用了一个
SwingWorker
,但您在其中调用了
textarea.append(“Done!\n”)
。这非常糟糕

您的
过程
方法需要能够做到这一点……基本上,当您
过程
接收到
100
时,它应该能够更新文本区域


您还可以允许在其他位置处理异常,允许
doInBackground
方法抛出异常。这将允许您使用
done
方法和
get
方法确定是否发生了异常,其附加好处是
done
在EDT的po中调用一个或多个close语句正在挂起或引发新异常(可能也来自task.cancel)。请尝试将JOptionPane移到这些语句上方。如果这样做有效,请将每个close语句隔离在其中。在本网站上发布代码时,请考虑使用更好的代码缩进和包装。如果我们可以轻松阅读您的代码,我们就可以更容易地理解它,并能够更好地帮助您。您的代码片段表明您可能是与Swing的线程规则相冲突。根据这段代码很难知道。另外,为JOptionPane创建ghost JFrame的目的是什么?MadProgrammer,谢谢你的帮助。将JOptionPane移到上面可以让垃圾神帮你格式化代码。