本文介绍了JavaFX预加载器不更新进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了JavaFX Preloader的问题。在启动阶段,应用程序必须连接到数据库并读取很多,所以我认为在此期间显示启动画面会很好。问题是ProgressBar自动达到100%,我不明白为什么。

I'm having trouble with the JavaFX Preloader. During the start phase the application will have to connect to a DB and read many so I thought it would be nice to display a splash screen during this time. The problem is the ProgressBar automaticly goes to 100% and I don't understand why.

应用程序类。线程休眠将在以后被实际代码替换(数据库连接等)

Application class. Thread sleep will be replaced by real code later (DB connection etc)

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

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());
}

出于某种原因,我得到以下输出:

For some reason I get the following output:

进度0.0
进度1.0

Progress 0.0Progress 1.0

推荐答案

我有同样的问题,我找到了解决方案两小时的搜索和5分钟的仔细阅读JavaDoc。:)

I had same problem and I found solution after two hours of searching and 5 minutes of carefully reading of JavaDoc.:)

通过 notifyPreloader()方法发送的通知只能通过 Preloader.handleApplicationNotification()方法处理,并且发送哪种类型的通知无关紧要。

Notifications send by notifyPreloader() method can be handled only by Preloader.handleApplicationNotification() method and it doesn't matter which type of notification are you sending.

所以改变你这样的代码:

So change you code like this:

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());
          }
    }
}

这篇关于JavaFX预加载器不更新进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-01 02:27