问题描述
我只是在玩Java的ProgressMonitorInputStream来监视流经BufferedInputStream的数据.这是我目前正在尝试的代码:
I'm just playing about with Java's ProgressMonitorInputStream to monitor data as it flows through a BufferedInputStream. Here is the code I'm currently trying:
InputStream in = new BufferedInputStream(
new ProgressMonitorInputStream(
new JFrame(),"Scanning",new FileInputStream(dir.getSearchInputFile())));
这工作得很好,并弹出一个带有进度条的新JFrame窗口,其中显示了输入流的进度.
This works perfectly fine, and pops up a new JFrame window with a progress bar that displays the progress of the input stream.
反正有没有让ProgressMonitorInputstream更新另一个JFrame中存在的现有JProgressBar?
Is there anyway of getting the ProgressMonitorInputstream to update an existing JProgressBar that exists within another JFrame?
我尝试了各种方法,例如使用构造函数传递JProgressBar或尝试在参数中指定框架.每次尝试时,我都会得到一个新的JFrame.
I've tried various methods, such as passing in a JProgressBar with the constructor, or trying to specify the frame within the parameters. Each time I try, I just get a new JFrame.
我要把这全部弄错吗?
任何输入将不胜感激.
谢谢
推荐答案
正在监视其进度的SwingWorker
不在乎谁在听.只需在界面中实现 PropertyChangeListener
接口其他框架,并将侦听器添加到工作程序,如 ProgressMonitorDemo
.
The SwingWorker
whose progress is being monitored doesn't care who's listening. Simply implement the PropertyChangeListener
interface in the other frame and add the listener to the worker, as shown in ProgressMonitorDemo
.
附录:作为一个具体示例,在演示中添加进度条:
Addendum: As a concrete example, add a progress bar to the demo:
private JProgressBar bar = new JProgressBar();
将其添加到面板的构造函数中:
Add it in the panel's constructor:
this.add(bar);
在面板的propertyChange()
侦听器中更新它:
Update it in the panel's propertyChange()
listener:
bar.setValue(progress);
这篇关于使用现有JProgressBar的Java ProgressMonitorInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!