且构网

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

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

更新时间:2021-09-09 21:52:49

我可以想到两种方式.

首先,将代码复制包装到 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)