gpt4 book ai didi

java - SwingWorker 没有执行 doInBackGround()

转载 作者:行者123 更新时间:2023-11-30 07:02:40 24 4
gpt4 key购买 nike

我正在从事的项目需要一名 Swing worker ,因此我尝试使用它。我试过如下所示使用它,但是,这个程序只产生“Doing swing stuff”输出。

如何让SwingWorker完成doInBackGround方法?

import javax.swing.SwingWorker;

public class WorkerTest {

public static void main(String[] args) {
Worker a = new Worker();
a.execute();
System.out.println("Doing swing stuff");

}

static class Worker extends SwingWorker<Void, Void> {

protected void done() {
System.out.println("done");
}

@Override
protected Void doInBackground(){
try {
System.out.println("working");
Thread.sleep(5000);

} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}

最佳答案

改变:

public static void main(String[] args) {
Worker a = new Worker();
a.execute();
System.out.println("Doing swing stuff");
}

收件人:

public static void main(String[] args) {
Worker a = new Worker();
a.execute();
System.out.println("Doing swing stuff");
JOptionPane.showConfirmDialog(null, "Cancel", "Cancel this task?", JOptionPane.DEFAULT_OPTION);
}

选项 Pane (打开)使事件调度线程保持 Activity 状态。

关于java - SwingWorker 没有执行 doInBackGround(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28929059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com