首先,我最近一直在使用 Java 的 Concurrency 包,但我发现了一个我被困住的问题。我想拥有和Application,并且Application可以拥有一个SplashScreen
带有状态栏和加载其他数据的。所以我决定使用SwingUtilities.invokeAndWait( call the splash component here )
. 然后SplashScreen
出现一个JProgressBar
并运行一组线程。但我似乎无法很好地处理事情。我已经查看SwingWorker
并尝试将其用于此目的,但线程刚刚返回。这是一些伪代码。以及我想要达到的目标。
- 有一个
SplashScreen
在加载信息时暂停的应用程序 - 能够在多个线程下运行
SplashScreen
- 在所有线程完成之前,让 Update-able的进度条
SplashScreen
不退出。
启动闪屏
try {
SwingUtilities.invokeAndWait( SplashScreen );
} catch (InterruptedException e) {
} catch (InvocationTargetException e) { }
闪屏结构
SplashScreen extends JFrame implements Runnable{
public void run() {
//run threads
//while updating status bar
}
}
我尝试了很多东西,包括SwingWorkers
使用 CountDownLatch 的线程等。CountDownLatch 实际上以我想要处理的方式工作,但我无法更新 GUI。使用时,SwingWorkers
要么invokeAndWait
基本上无效(这是他们的目的),要么即使使用PropertyChangedListener
. 如果其他人有一些想法,很高兴听到它们。提前致谢。
我实际上已经准备好发布更好的代码来帮助并找到我的解决方案。我感谢所有帮助过的人。