【发布时间】:2013-02-02 10:16:36
【问题描述】:
我正在设计一个简单的 JavaFX 表单。
首先,我加载 JavaFX 环境(并等待它完成),如下所示:
final CountDownLatch latch_l = new CountDownLatch(1);
try {
// init the JavaFX environment
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFXPanel(); // init JavaFX
latch_l.countDown();
}
});
latch_l.await();
}
这很好用。 (我之所以需要先这样加载JavaFX,是因为它主要是一个Swing应用,里面有一些JavaFX组件,不过是后面加载的)
现在,我想在启动时添加一个启动画面,并在 JavaFX 环境加载时显示它(实际上在屏幕上显示 5 秒,因为有徽标、商标等。我需要展示的应用程序)
所以我想出了一个 SplashScreen 类,它只在屏幕上显示一个 JWindow,就像这样:
public class SplashScreen {
protected JWindow splashScreen_m = new JWindow();
protected Integer splashScreenDuration_m = 5000;
public void show() {
// fill the splash-screen with informations
...
// display the splash-screen
splashScreen_m.validate();
splashScreen_m.pack();
splashScreen_m.setLocationRelativeTo(null);
splashScreen_m.setVisible(true);
}
public void unload() {
// unload the splash-screen
splashScreen_m.setVisible(false);
splashScreen_m.dispose();
}
}
现在,我希望启动画面加载并显示自身 5 秒。 同时,我也想加载 JavaFX 环境。
所以我像这样更新了 CountDownLatch:
final CountDownLatch latch_l = new CountDownLatch(2); // now countdown is set to 2
final SplashScreen splash_l = new SplashScreen();
try {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// show splash-screen
splash_l.show();
latch_l.countDown();
// init the JavaFX environment
new JFXPanel(); // init JavaFX
latch_l.countDown();
}
});
latch_l.await();
splash_l.unload();
}
所以,它正在工作,但启动画面只停留在 JavaFX 环境加载,所以基本上它卸载得非常快(这是正常的,考虑到我编写的代码)。
如何在不冻结 EDT 的情况下至少显示 5 秒的初始屏幕(如果 JavaFX 加载速度更快)?
谢谢。
【问题讨论】:
-
为什么不使用(基于 AWT)
SplashScreen? -
这么多 EDT 违规 8O
-
@AndrewThompson :因为它不足以满足应用程序的需要
-
@MadProgrammer:是的,也许:/我是 GUI Java 编程的新手,所以...
-
具体为什么? "// 用信息填充启动画面..." 有点模糊,但其余的可以通过 AWT 启动 API 来完成。
标签: java multithreading swing splash-screen countdownlatch