我看不到您曾经在属性更改侦听器中设置过jLabel的文本,但是已经说过,您不应该这样做,因为它违反了OOP封装。而是:
-
把你的
DownloadPanel
A级
public void setPercentDownload(int value)
方法,在此方法中,设置jProgressBar
和
jLabel的文本通过
setText(...)
-
在prop change listener中,调用此方法将值传递给GUI。
-
不要在监听器中设置进度条的值,让视图像我上面提到的那样做。
-
请确保在属性更改侦听器中侦听newValue==swingWorker.StateValue.Done。发生这种情况时,请致电
get()
在您的工作线程上,这样您就可以捕获后台线程中可能发生的任何异常。
-
附带问题:只对说“不”
null
布局和设定界限。它们使刚性的GUI不适用于所有平台,并且是调试和增强的一个负担。使用布局管理器。
例如,我的
Minimal, Complete, and Verifiable example
(mcve):
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.*;
import java.io.Serializable;
import java.util.concurrent.ExecutionException;
import javax.swing.*;
public class FooProgress {
private static void createAndShowGui() {
JFrame frame = new JFrame("FooProgress");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new DownloadPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class DownloadListener implements PropertyChangeListener {
private DownloadPanel downloadPanel;
public DownloadListener(DownloadPanel downloadPanel) {
this.downloadPanel = downloadPanel;
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if ("progress".equals(evt.getPropertyName())) {
int value = (int) evt.getNewValue();
downloadPanel.setPercentDownload(value);
} else if ("state".equals(evt.getPropertyName())) {
if (evt.getNewValue() == SwingWorker.StateValue.DONE) {
Download download = (Download) evt.getSource();
try {
download.get();
} catch (InterruptedException | ExecutionException e) {
// TODO: handle exceptions here
e.printStackTrace();
}
}
}
}
}
@SuppressWarnings("serial")
class DownloadPanel extends JPanel {
private static final String PROGRESS_FORMAT = "%03d%%";
private JProgressBar jpb = new JProgressBar(0, 100);
private JLabel downloadedSizeLabel = new JLabel(String.format(PROGRESS_FORMAT, 0));
private DownLoadAction downLoadAction = new DownLoadAction();
public DownloadPanel() {
JPanel topPanel = new JPanel();
topPanel.add(new JLabel("Download Progress:"));
topPanel.add(downloadedSizeLabel);
JPanel bottomPanel = new JPanel();
bottomPanel.add(new JButton(downLoadAction));
setLayout(new BorderLayout());
add(topPanel, BorderLayout.PAGE_START);
add(jpb);
add(bottomPanel, BorderLayout.PAGE_END);
}
public void setPercentDownload(int value) {
downloadedSizeLabel.setText(String.format(PROGRESS_FORMAT, value));
jpb.setValue(value);
if (value == 100) {
downLoadAction.setEnabled(true);
}
}
private class DownLoadAction extends AbstractAction {
public DownLoadAction() {
super("Download");
putValue(MNEMONIC_KEY, KeyEvent.VK_D);
}
@Override
public void actionPerformed(ActionEvent e) {
setEnabled(false);
setPercentDownload(0);
Download download = new Download();
DownloadListener listener = new DownloadListener(DownloadPanel.this);
download.addPropertyChangeListener(listener);
download.execute();
}
}
}
class Download extends SwingWorker<Void, Void> implements Serializable, Runnable {
private static final long SLEEP_TIME = 200;
public Void doInBackground() throws Exception {
int myProgress = 0;
while (myProgress < 100) {
myProgress += (int) (10 * Math.random());
myProgress = Math.min(myProgress, 100);
setProgress(myProgress);
Thread.sleep(SLEEP_TIME);
}
return null;
}
}
将来,您将希望用您的代码创建和发布mcve,这样我们就可以轻松地编译、运行和测试它。您的代码中的文件处理太多,我们目前无法测试。我用thread.sleep替换了90%的代码。