且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在文件传输时创建进度条

更新时间:2021-09-09 21:53:01

我可以想到两种方式。

首先将复制代码包装到 SwingWorker ,使用 setProgress 方法更新进度和属性更改侦听器监视progress属性的更改。

Start by wrapping you copy code into a SwingWorker, using the setProgress method to update the progress and a property change listener to monitor changes to the progress property.

当progress属性发生更改时,您将更新UI。

When the progress property changes, you would then update the UI.

此解决方案将要求您提供自己的UI

This solution will require you to supply you own UI

使用 ProgressMonitorInputStream ,它附带了自己的用户界面。

Use a ProgressMonitorInputStream, which comes with it's own UI.

InputStream in = new BufferedInputStream(
    new ProgressMonitorInputStream(
        parentComponent,
        "Reading " + fileName,
        new FileInputStream(fileName)));

(从Java Docs中窃取的示例)

(Example stolen from Java Docs)