Java 如何更改进度条和标签的值?
只有在方法结束时,Java 如何更改进度条和标签的值?,java,swing,event-dispatch-thread,Java,Swing,Event Dispatch Thread,只有在方法结束时,JLabel和JProgressBar才不会更改其值 this.desSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { DownloadXml(); } }); private void DownloadXml() throws Exception { Integer a = 4
JLabel
和JProgressBar
才不会更改其值
this.desSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DownloadXml();
}
});
private void DownloadXml() throws Exception {
Integer a = 456;
int value = (1000 / a) - 1;
this.numDes.setText("0" + "/" + a);
for (int i = 0; i < a; i++) {
saveXml(ligas.get(i),i,path);
this.numDes.setText(i + "/" + a); //this is a jlabel
this.progressbar.setValue(value * i); //jprogressbar
}
}
private void SaveXml(String xml,int a,String path) throws IOException {
}
this.desSave.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
下载xml();
}
});
私有void DownloadXml()引发异常{
整数a=456;
整数值=(1000/a)-1;
this.numDes.setText(“0”+“/”+a);
for(int i=0;i
您正在阻止事件分派线程,这将阻止它更新UI。您可以使用线程
,但Swing是单线程API,这意味着只能在事件调度线程的上下文中对UI进行更新
您可以使用SwingWorker
,它允许您在后台线程中执行长时间运行的进程,但支持安全地同步UI更新
例如,加上它的progress
和PropertyChange
支持,它变得易于管理
public class Worker extends SwingWorker<Object, Object> {
@Override
protected Object doInBackground() throws Exception {
// The download code would go here...
for (int index = 0; index < 1000; index++) {
int progress = Math.round(((float)index / 1000f) * 100f);
setProgress(progress);
Thread.sleep(10);
}
// You could return the down load file if you wanted...
return null;
}
}
您可以使用对EDT的
发布
/过程
支持和更新、属性更改
支持或worker's done方法,在安全完成工作后,从EDT中获取工作程序的结果您正在阻止事件调度线程,从而阻止它更新UI。您可以使用线程
,但Swing是单线程API,这意味着只能在事件调度线程的上下文中对UI进行更新
您可以使用SwingWorker
,它允许您在后台线程中执行长时间运行的进程,但支持安全地同步UI更新
例如,加上它的progress
和PropertyChange
支持,它变得易于管理
public class Worker extends SwingWorker<Object, Object> {
@Override
protected Object doInBackground() throws Exception {
// The download code would go here...
for (int index = 0; index < 1000; index++) {
int progress = Math.round(((float)index / 1000f) * 100f);
setProgress(progress);
Thread.sleep(10);
}
// You could return the down load file if you wanted...
return null;
}
}
您可以使用对EDT的
发布
/过程
支持和更新、属性更改
支持或worker's done方法,在安全完成工作后,从EDT中获取工作程序的结果您正在阻止事件调度线程,从而阻止它更新UI。您可以使用线程
,但Swing是单线程API,这意味着只能在事件调度线程的上下文中对UI进行更新
您可以使用SwingWorker
,它允许您在后台线程中执行长时间运行的进程,但支持安全地同步UI更新
例如,加上它的progress
和PropertyChange
支持,它变得易于管理
public class Worker extends SwingWorker<Object, Object> {
@Override
protected Object doInBackground() throws Exception {
// The download code would go here...
for (int index = 0; index < 1000; index++) {
int progress = Math.round(((float)index / 1000f) * 100f);
setProgress(progress);
Thread.sleep(10);
}
// You could return the down load file if you wanted...
return null;
}
}
您可以使用对EDT的
发布
/过程
支持和更新、属性更改
支持或worker's done方法,在安全完成工作后,从EDT中获取工作程序的结果您正在阻止事件调度线程,从而阻止它更新UI。您可以使用线程
,但Swing是单线程API,这意味着只能在事件调度线程的上下文中对UI进行更新
您可以使用SwingWorker
,它允许您在后台线程中执行长时间运行的进程,但支持安全地同步UI更新
例如,加上它的progress
和PropertyChange
支持,它变得易于管理
public class Worker extends SwingWorker<Object, Object> {
@Override
protected Object doInBackground() throws Exception {
// The download code would go here...
for (int index = 0; index < 1000; index++) {
int progress = Math.round(((float)index / 1000f) * 100f);
setProgress(progress);
Thread.sleep(10);
}
// You could return the down load file if you wanted...
return null;
}
}
您可以使用对EDT的
发布
/过程
支持和更新、属性更改
支持或worker's done方法在安全完成后获得工作人员的结果,在EDT内使用SwingWorker,它的进度和属性更改支持您应该使用线程。例如,使用SwingWorker,它的进度和属性更改支持您应该使用线程。对于,使用SwingWorker,它的进度和属性更改支持您应该使用线程。对于,使用SwingWorker,它的进度和属性更改支持您应该使用线程。对于,