我遇到了JavaFX Preloader的问题。在启动阶段,应用程序必须连接到数据库并读取很多,所以我认为在此期间显示启动画面会很不错。问题是ProgressBar自动达到100%,我不明白为什么。
应用程序类。线程休眠将在以后用实际代码替换(DB连接等)
public void init() throws InterruptedException
{
notifyPreloader(new Preloader.ProgressNotification(0.0));
Thread.sleep(5000);
notifyPreloader(new Preloader.ProgressNotification(0.1));
Thread.sleep(5000);
notifyPreloader(new Preloader.ProgressNotification(0.2));
}
Preloader
public class PreloaderDemo extends Preloader {
ProgressBar bar;
Stage stage;
private Scene createPreloaderScene() {
bar = new ProgressBar();
bar.getProgress();
BorderPane p = new BorderPane();
p.setCenter(bar);
return new Scene(p, 300, 150);
}
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
stage.setScene(createPreloaderScene());
stage.show();
}
@Override
public void handleStateChangeNotification(StateChangeNotification scn) {
if (scn.getType() == StateChangeNotification.Type.BEFORE_START) {
stage.hide();
}
}
@Override
public void handleProgressNotification(ProgressNotification pn) {
bar.setProgress(pn.getProgress());
System.out.println("Progress " + bar.getProgress());
}
出于某种原因,我得到以下输出:
进展0.0 Progress 1.0
答案 0 :(得分:7)
我有同样的问题,经过两个小时的搜索和5分钟仔细阅读JavaDoc后我找到了解决方案。:)
notifyPreloader()
方法发送的通知只能通过Preloader.handleApplicationNotification()
方法处理,并且您发送的通知类型无关紧要。
所以改变你这样的代码:
public class PreloaderDemo extends Preloader {
.... everything like it was and add this ...
@Override
public void handleApplicationNotification(PreloaderNotification arg0) {
if (arg0 instanceof ProgressNotification) {
ProgressNotification pn= (ProgressNotification) arg0;
bar.setProgress(pn.getProgress());
System.out.println("Progress " + bar.getProgress());
}
}
}