【问题标题】:JavaFX preloader not updating progressJavaFX 预加载器不更新进度
【发布时间】:2013-12-20 10:39:01
【问题描述】:

我在使用 JavaFX 预加载器时遇到问题。在启动阶段,应用程序必须连接到数据库并读取许多数据,因此我认为在此期间显示启动屏幕会很好。问题是 ProgressBar 自动转到 100%,我不明白为什么。

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

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

预加载器

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 进展 1.0

【问题讨论】:

    标签: javafx


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多