我的swingworker不会重新绘制我的进度条(我有2个课程)。
这是我的文件下载代码。它将下载百分比放在进度栏中。
public class Downloader extends SwingWorker<String, Integer> {
private String fileURL, destinationDirectory;
private int fileTotalSize;
public void DownloaderF(String file, String dir) {
this.fileURL = file;
this.destinationDirectory = dir;
}
@Override
protected String doInBackground() throws Exception {
try {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
String downloadedFileName = fileURL.substring(fileURL.lastIndexOf("/")+1);
int filesize = httpConn.getContentLength();
int responseCode = httpConn.getResponseCode();
byte[] buffer = new byte[4096];
int bytesRead = 0;
int i = 0;
int total = 0;
BufferedInputStream in = new BufferedInputStream(httpConn.getInputStream());
FileOutputStream fos = new FileOutputStream(destinationDirectory + File.separator + downloadedFileName);
BufferedOutputStream bout = new BufferedOutputStream(fos,4096);
while ((i=in.read(buffer,0,4096))>=0) {
total = total + i;
bout.write(buffer,0,i);
fileTotalSize = (total * 100) / filesize;
publish(fileTotalSize);
}
bout.close();
in.close();
} catch(FileNotFoundException FNFE) {
System.out.print("HTTP: 404!");
} catch (IOException ex) {
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
@Override
protected void process(List<Integer> chunks) {
try {
Skin barValue = new Skin();
barValue.setBar(fileTotalSize);
//System.out.print("PROCESS:" + fileTotalSize + "\n");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
这是我的按钮代码和进度条值更改方法:
private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
// Дебаг
Downloader downloadFile = new Downloader();
downloadFile.DownloaderF("http://ipv4.download.thinkbroadband.com/100MB.zip", ".");
downloadFile.execute();
}
public void setBar(int Value) {
DownloadBar.setValue(Value);
DownloadBar.repaint();
System.out.print("1\n");
}
将打印“1 \ n”,但进度条不会移动。
抱歉我的英语不好。
答案 0 :(得分:2)
最有可能的是,您的问题来自这一行:
Skin barValue = new Skin();
您正在重新创建Skin
类的新实例,而不是引用已存在的实例。因此,您很可能指出可能甚至没有显示的内容,因此您不会发现任何事情。
正确的方法是向您的班级Downloader
提供对原始Skin
的引用,其中包含您显示的&#34;进度条&#34;。
供参考:
JProgressBar
上调用repaint()(进度条将为您执行此操作)以下是从您的代码中派生的示例代码(虽然我制作了一些快捷方式)实际上可以按预期正常工作:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class Skin {
private JProgressBar DownloadBar;
public static class Downloader extends SwingWorker<String, Integer> {
private final String fileURL, destinationDirectory;
private int fileTotalSize;
private final Skin barValue;
public Downloader(Skin skin, String file, String dir) {
this.barValue = skin;
this.fileURL = file;
this.destinationDirectory = dir;
}
@Override
protected String doInBackground() throws Exception {
try {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
String downloadedFileName = fileURL.substring(fileURL
.lastIndexOf("/") + 1);
int filesize = httpConn.getContentLength();
int responseCode = httpConn.getResponseCode();
byte[] buffer = new byte[4096];
int bytesRead = 0;
int i = 0;
int total = 0;
BufferedInputStream in = new BufferedInputStream(
httpConn.getInputStream());
FileOutputStream fos = new FileOutputStream(
destinationDirectory + File.separator
+ downloadedFileName);
BufferedOutputStream bout = new BufferedOutputStream(fos, 4096);
while ((i = in.read(buffer, 0, 4096)) >= 0) {
total = total + i;
bout.write(buffer, 0, i);
fileTotalSize = total * 100 / filesize;
publish(fileTotalSize);
}
bout.close();
in.close();
} catch (FileNotFoundException FNFE) {
System.out.print("HTTP: 404!");
} catch (IOException ex) {
Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE,
null, ex);
}
return null;
}
@Override
protected void process(List<Integer> chunks) {
barValue.setBar(fileTotalSize);
}
}
private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {
Downloader downloadFile = new Downloader(this,
"http://ipv4.download.thinkbroadband.com/100MB.zip", ".");
downloadFile.execute();
}
protected void initUI() throws MalformedURLException {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton login = new JButton("Login");
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loginButtonActionPerformed(e);
}
});
DownloadBar = new JProgressBar();
frame.add(login, BorderLayout.NORTH);
frame.add(new JLabel(new ImageIcon(new URL(
"http://home.scarlet.be/belperret/images/image1.jpg"))));
frame.add(DownloadBar, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public void setBar(int Value) {
DownloadBar.setValue(Value);
DownloadBar.repaint();
System.out.println("1");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
try {
new Skin().initUI();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
}
答案 1 :(得分:0)
您正在正确执行publish(fileTotalSize)
,因此我认为它可能是您的流程代码。
试试这个改变:
protected void process(List<Integer> chunks) {
try {
Skin barValue = new Skin();
barValue.setBar( chunks.get(0) );
//System.out.print("PROCESS:" + fileTotalSize + "\n");
} catch (Exception ex) {
ex.printStackTrace();
}
}
我没有完整的代码,因此我无法对其进行测试。