我的swingworker不會重新繪製我的進度條(我有2個類)。Java swing,SwingWorker,進程欄不會更新
這是我的文件下載程序代碼。它將百分比下載進度欄中。
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」個將被打印,但進度條不會移動。
對不起,我的英語不好。
你有什麼minValue(最小值)和進度條的包括maxValue –
分鐘= 0; max = 100 – user3590378
你的'fileTotalSize'是int,但是你推入一個float,檢查這個並在控制檯上打印天氣是否正確(而不是打印1 \ n打印值+「\ n」以進行測試)。 –