2012-07-21 85 views
2

問題已解決!!!!!! 非常感謝trashgod和HoverCraftFullOfEels!我終於通過使用下面的例子並略微改變了它的概念。該更改允許縮放進度條(默認爲100個單位)。 再次感謝您的耐心和意願。意味着很多人, 〜凱特下載期間使用swingworker更新JProgressBar

PS - + 1的所有「圓:)

/** @see http://stackoverflow.com/questions/4637215 */ 
public class Threading_01 extends JFrame { 

private static final String s = "0.00"; 
private JProgressBar progressBar = new JProgressBar(0, 100); 
private JLabel label = new JLabel(s, JLabel.CENTER); 

public Threading_01() { 
    this.setLayout(new GridLayout(0, 1)); 
    this.setTitle("√2"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.add(progressBar); 
    this.add(label); 
    this.setSize(161, 100); 
    this.setLocationRelativeTo(null); 
    this.setVisible(true); 
} 

public void runCalc() { 
// progressBar.setIndeterminate(true); 
    TwoWorker task = new TwoWorker(); 
    task.addPropertyChangeListener(new PropertyChangeListener() { 

     @Override 
     public void propertyChange(PropertyChangeEvent e) { 
      if ("progress".equals(e.getPropertyName())) { 
       progressBar.setIndeterminate(false); 
       progressBar.setValue((Integer) e.getNewValue()); 
       System.out.println("**: " + e.getNewValue()); 
      } 
     } 
    }); 
    task.execute(); 
} 

private class TwoWorker extends SwingWorker<Double, Double> { 

    private static final int N = 734; 
    private final DecimalFormat df = new DecimalFormat(s); 

    @Override 
    protected Double doInBackground() throws Exception { 
     int cntr = 1; // 
     double d1; 
     double d2; 
     double d3; 
     for (int i = 0; i <=N; i++) { 
      d1 = N; 
      d2 = i; 
      d3 = d2/d1; 
      d3 = d3 * 100; 
      System.out.println(i + "++ " + d3); 
      if(d3 >= cntr){ 
       System.out.println(i + "UPDATE"); 
       cntr++; 
       setProgress(cntr); 
       publish(Double.valueOf(cntr)); 
       Thread.sleep(250); // simulate latency 
      } 
     } 
     return Double.valueOf(0); 
    } 

    @Override 
    protected void process(List<Double> chunks) { 
     for (double d : chunks) { 
      label.setText(df.format(d)); 
      System.out.println(": " + d); 
     } 
    } 
} 

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      Threading_01 t = new Threading_01(); 
      t.runCalc(); 
     } 
    }); 
} 

}

+5

您可以將您當前的嘗試發佈到您想要做什麼的簡單可編譯和可運行的版本中,並完成您嘗試使用SwingWorker的過程,最好是[sscce](http://sscce.org)if可能?要麼就是要麼詳細說明你卡在哪裏,因爲我從上面得到的結果是你完全陷在了一切。如果是這樣的話,那麼你需要重新審查教程。但無論如何,請讓我們知道! – 2012-07-21 17:06:56

+2

指向示例的其他鏈接(我的!)包括[this one](http://stackoverflow.com/a/5533581/522444)和[this other one](http://stackoverflow.com/a/10240173/522444) 。 – 2012-07-21 17:13:19

+1

@ HFoE的[示例](http://stackoverflow.com/a/5533581/522444)演示了worker如何擴展它對[_observer pattern_]的使用(http://stackoverflow.com/a/5533581/522444) ;兩者皆爲+1。您可能想要修改他或我引用的[示例](http://stackoverflow.com/a/11594200/230513)中的一個來構建您的[sscce](http://sscce.org/)。 – trashgod 2012-07-21 17:26:22

回答

5

更新進度條在PropertyChangeListener通常處理,如圖所示here。另見相關的example。如果您的工作人員稍後可以提供更明確的進度,則調用setIndeterminate(true)是一個合理的初始設置。