我有一个带有进度条和文本区域的模态 Jdialog。我正在启动一个 Swing Worker 来执行一些后台任务并使用发布过程更新 jdialog 中的文本区域。但是,在运行程序时,我观察到即使调用了 process 方法,它仍然没有更新 jdialog 中的文本区域。另外,请注意,我在 swing worker 的 done 方法中关闭了 jdialog,它工作正常。谁能告诉我为什么没有从(SwingWorker 的)流程方法进行 gui 更新?
JDialog 类 -
public class ProgressDialog extends JDialog {
private static final long serialVersionUID = 1L;
private GridBagLayout gridBag;
private GridBagConstraints constraints;
private ProgressDialog.ProgressBar progressBar;
private JScrollPane scrollPane;
private JTextArea textArea;
ProgressDialog(Frame owner, String title, boolean modal, int numTasks) {
super(owner,title,modal);
gridBag = new GridBagLayout();
constraints = new GridBagConstraints();
this.progressBar = new ProgressDialog.ProgressBar();
this.progressBar.init(numTasks);
this.textArea = new JTextArea(10,30);
this.textArea.setEditable(false);
this.scrollPane = new JScrollPane(this.textArea);
this.scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
this.setLayout(gridBag);
constraints.gridx = 0;
constraints.gridy = 0;
gridBag.setConstraints(progressBar, constraints);
constraints.gridx = 0;
constraints.gridy = 1;
constraints.insets = new Insets(10,0,10,0);
gridBag.setConstraints(scrollPane, constraints);
this.add(progressBar);
this.add(scrollPane);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setSize(400, 300);
this.setLocationRelativeTo(null);
}
class ProgressBar extends JProgressBar {
ProgressBar() {
}
void init(int numTasks) {
setMaximum(numTasks);
setStringPainted(true);
setValue(0);
}
void setProgress(int taskCompleted) {
int totalTasks = getMaximum();
setValue(taskCompleted);
setString(taskCompleted+"/"+totalTasks);
}
}
达令说
郎朗坤
相关分类