2014-04-30 46 views
1

我的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」個將被打印,但進度條不會移動。

對不起,我的英語不好。

+0

你有什麼minValue(最小值)和進度條的包括maxValue –

+0

分鐘= 0; max = 100 – user3590378

+0

你的'fileTotalSize'是int,但是你推入一個float,檢查這個並在控制檯上打印天氣是否正確(而不是打印1 \ n打印值+「\ n」以進行測試)。 –

回答

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(); 
    } 
} 

我沒有完整的代碼,所以我無法測試出來。

+0

感謝您的回答,但我嘗試過。 – user3590378

2

最有可能的,你的問題來源於此行:

Skin barValue = new Skin(); 

你現在重新創建你的新實例Skin類,而不是引用一個已經存在的。因此,你很可能指向可能甚至沒有顯示的東西,因此你沒有看到任何事情發生。

正確的做法是向您的課程Downloader提供包含顯示的「進度欄」的原始Skin的引用。

FYI:

  • 無需調用一個JProgressBar重繪()當您更改它的值(進度條會爲你做它)
  • 請遵循Java的命名約定(即,方法名變量必須以小寫字母開頭):對於有經驗的用戶,您的代碼難以閱讀。

這裏是你得到的(雖然我做了幾個快捷鍵)的樣本代碼,實際工作正常預期:

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(); 
       } 
      } 
     }); 
    } 
} 
+0

感謝您的回答,但我有2班,我需要創建實例。 – user3590378

+0

@ user3590378:發佈[SSCCE](http://sscce.org),如果您想要更好的幫助。我可以告訴你的是,創建一個新實例是個問題,通過參考就是解決方案。這只是一個你不明白什麼意思的問題。順便說一句,我的解釋還包含兩個類。 –