SwingWorker
为此非常理想。下面的示例在后台执行一个简单的迭代,同时在一个窗口中报告进度和中间结果。你可以在合适的
SwingWorker
构造函数中传递所需的任何参数。
import java.awt.EventQueue;import java.awt.GridLayout;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.text.DecimalFormat;import java.util.List;import javax.swing.*;public class TwoRoot extends Jframe { private static final String s = "0.000000000000000"; private JProgressBar progressBar = new JProgressBar(0, 100); private JLabel label = new JLabel(s, JLabel.CENTER); public TwoRoot() { 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()); } } }); task.execute(); } private class TwoWorker extends SwingWorker<Double, Double> { private static final int N = 5; private final DecimalFormat df = new DecimalFormat(s); double x = 1; @Override protected Double doInBackground() throws Exception { for (int i = 1; i <= N; i++) { x = x - (((x * x - 2) / (2 * x))); setProgress(i * (100 / N)); publish(Double.valueOf(x)); Thread.sleep(1000); // simulate latency } return Double.valueOf(x); } @Override protected void process(List<Double> chunks) { for (double d : chunks) { label.setText(df.format(d)); } } } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { TwoRoot t = new TwoRoot(); t.runCalc(); } }); }}