在我的代码中,用户按下一个按钮,然后我希望它调用一个 SwingWorker 后台线程,我基本上循环遍历一个列表,将其与其他数据交叉引用,然后使用 JLabel 为每个更新 GUI通过我的循环。问题是我知道我应该在 doInBackground() 中执行此操作,但我没有任何可返回的内容,而且我还想在每次循环时使用新的 JLabel 更新 JPanel。我该怎么做呢?谢谢!
最佳答案
这是一个我非常喜欢的完整示例 Swing Worker Example
您必须使用publish()
并覆盖process()
示例:
class Worker extends SwingWorker<Void, String> {
@Override
protected void doInBackground() throws Exception {
//here you make heavy task this is running in another thread not in EDT
//process after some time call publish()
}
@Override
protected void process(List<String> chunks) {
//this is executed in the EDT
//here you update your label
}
}
关于java - 使用 Swingworker 不断更新 GUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18449995/