2017-10-12 4 views
1

J'essaie de faire apparaître mon écran de démarrage JavaFX Preloader avant mon application. J'utilise Eclipse IDE et quand je clique sur "Exécuter", la moitié du temps l'écran de démarrage s'affichera correctement et l'autre moitié du temps j'obtiendrai un écran gris ou noir au lieu d'où l'image devrait être.Pourquoi mon préchargeur JavaFX apparaît-il parfois gris/noir et d'autres fois il charge correctement?

Je ne sais pas quel est le problème pour que l'affichage ne s'affiche correctement parfois.

SplashController:

public class SplashController extends Preloader { 
    private static final double WIDTH = 676; 
    private static final double HEIGHT = 227; 
    private Stage preloaderStage; 
    private Label progressText; 
    private Pane splashScreen; 

public SplashController() {}  

@Override 
    public void init() throws Exception { 
    ImageView splash = 
     new ImageView(new Image(Demo.class.getResource("pic.png").toString())); 
    progressText = 
     new Label("VERSION: " + getVersion() + " ~~~ Loading plugins, please wait..."); 
    splashScreen = new VBox(); 
    splashScreen.getChildren().addAll(splash, progressText); 
    progressText.setAlignment(Pos.CENTER); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
    this.preloaderStage = primaryStage; 
    Scene splashScene = new Scene(splashScreen); 
    this.preloaderStage.initStyle(StageStyle.UNDECORATED); 
    final Rectangle2D bounds = Screen.getPrimary().getBounds(); 
    this.preloaderStage.setScene(splashScene); 
    this.preloaderStage.setX(bounds.getMinX() + bounds.getWidth()/2 - WIDTH/2); 
    this.preloaderStage.setY(bounds.getMinY() + bounds.getHeight()/2 - HEIGHT/2); 
    this.preloaderStage.show(); 
    } 
} 

Et puis dans ma classe principale Demo J'ai simplement:

public class Demo extends Application { 
    @Override 
    public void start(Stage stage) throws Exception { 
    FXMLLoader loader = new 
    FXMLLoader(Demo.class.getResource("FXMLDocument.fxml")); 
    GridPane root = loader.load(); 

        --------other app code here--------- 
    } 

    public static void main(String[] args) { 
    LauncherImpl.launchApplication(Demo.class, SplashController.class, args); 
    } 

}

+0

Merci! Oui le problème était que j'ai eu un long processus en cours sur le fil JavaFX :) – chip

+0

OK, chanceux devinez :-), je viens de faire le commentaire dans une réponse. – jewelsea

Répondre

0

Probablement, vous exécutez un processus en cours d'exécution à long sur le thread d'application JavaFX ou un thread impliqué dans le démarrage de l'application, qui empêche le bon fonctionnement du préchargeur.

Je vous suggère d'examiner un Oracle Preloader sample et de comparer à votre application. Assurez-vous que vous utilisez correctement les fonctionnalités concurrentes telles que Task, similaire à l'exemple lié. Vérifiez l'exemple de lien fonctionne dans votre environnement.

code source (juste copié à partir du lien exemple Oracle Preloader)

Note comment la méthode de début de la principale classe d'application LongAppInit, qu'un Task et le fil est donné naissance à faire en sorte que la longue initiation d'application ne pas avoir lieu sur le thread d'application JavaFX. Voir également comment la méthode d'application notifyPreloader() est appelée à différents moments dans l'initialisation de l'application longue pour informer le préchargeur de l'état actuel du processus d'initialisation afin qu'il puisse refléter la progression avec précision dans l'interface utilisateur en temps réel.

LongAppInitPreloader.java

public class LongAppInitPreloader extends Preloader { 
    ProgressBar bar; 
    Stage stage; 
    boolean noLoadingProgress = true; 

    private Scene createPreloaderScene() { 
     bar = new ProgressBar(0); 
     BorderPane p = new BorderPane(); 
     p.setCenter(bar); 
     return new Scene(p, 300, 150); 
    } 

    public void start(Stage stage) throws Exception { 
     this.stage = stage; 
     stage.setScene(createPreloaderScene()); 
     stage.show(); 
    } 

    @Override 
    public void handleProgressNotification(ProgressNotification pn) { 
     //application loading progress is rescaled to be first 50% 
     //Even if there is nothing to load 0% and 100% events can be 
     // delivered 
     if (pn.getProgress() != 1.0 || !noLoadingProgress) { 
      bar.setProgress(pn.getProgress()/2); 
      if (pn.getProgress() > 0) { 
       noLoadingProgress = false; 
      } 
     } 
    } 

    @Override 
    public void handleStateChangeNotification(StateChangeNotification evt) { 
     //ignore, hide after application signals it is ready 
    } 

    @Override 
    public void handleApplicationNotification(PreloaderNotification pn) { 
     if (pn instanceof ProgressNotification) { 
      //expect application to send us progress notifications 
      //with progress ranging from 0 to 1.0 
      double v = ((ProgressNotification) pn).getProgress(); 
      if (!noLoadingProgress) { 
       //if we were receiving loading progress notifications 
       //then progress is already at 50%. 
       //Rescale application progress to start from 50%    
       v = 0.5 + v/2; 
      } 
      bar.setProgress(v);    
     } else if (pn instanceof StateChangeNotification) { 
      //hide after get any state update from application 
      stage.hide(); 
     } 
    } 
} 

LongAppInit.java

public class LongInitApp extends Application { 
    Stage stage; 
    BooleanProperty ready = new SimpleBooleanProperty(false); 

    private void longStart() { 
     //simulate long init in background 
     Task task = new Task<Void>() { 
      @Override 
      protected Void call() throws Exception { 
       int max = 10; 
       for (int i = 1; i <= max; i++) { 
        Thread.sleep(200); 
        // Send progress to preloader 
        notifyPreloader(new ProgressNotification(((double) i)/max)); 
       } 
       // After init is ready, the app is ready to be shown 
       // Do this before hiding the preloader stage to prevent the 
       // app from exiting prematurely 
       ready.setValue(Boolean.TRUE); 

       notifyPreloader(new StateChangeNotification(
        StateChangeNotification.Type.BEFORE_START)); 

       return null; 
      } 
     }; 
     new Thread(task).start(); 
    } 

    @Override 
    public void start(final Stage stage) throws Exception { 
     // Initiate simulated long startup sequence 
     longStart(); 

     stage.setScene(new Scene(new Label("Application started"), 
      400, 400)); 

     // After the app is ready, show the stage 
     ready.addListener(new ChangeListener<Boolean>(){ 
      public void changed(
       ObservableValue<? extends Boolean> ov, Boolean t, Boolean t1) { 
        if (Boolean.TRUE.equals(t1)) { 
         Platform.runLater(new Runnable() { 
          public void run() { 
           stage.show(); 
          } 
         }); 
        } 
       } 
     });;     
    } 
}