【发布时间】:2017-04-21 20:36:48
【问题描述】:
我需要有人解释一下 Preloader 在 JavaFX 应用程序中的工作原理。我已经找到了生命周期,我知道首先调用 preloader 的 start(..) 然后调用 Application 的 start(..) 但这两个方法是从同一个线程调用的吗?我试图在应用程序的启动方法中制作一个简单的应用程序,将 Thread.sleep(8000) 放在预加载器的开头,我创建了一个简单的阶段并展示它。但是当我运行应用程序时,它只是在黑色阶段冻结,并且仅在 8 秒后正确显示预加载阶段,但为什么呢?
用源代码更新我的帖子:
公共类 PreloaderApp 扩展 Preloader {
public void init(){
System.out.println("Thread Preloader ID:"+ Thread.currentThread().getId());
System.out.println(" ------ ");
Pane effectRegion = LoaderFrame.getInstance().getEffectPane();
JFXFillTransition fill = new JFXFillTransition();
fill.setDuration(Duration.millis(400));
fill.setRegion(effectRegion);
fill.setAutoReverse(true);
fill.setCycleCount(Animation.INDEFINITE);
fill.setFromValue(Color.WHITE);
fill.setToValue(Color.rgb(0,77,147));
fill.play();
}
@Override
public void start(Stage primaryStage){
System.out.println("Thread Preloader(start) ID:"+ Thread.currentThread().getId());
System.out.println(" ------ ");
LoaderFrame.getInstance().show();
}
}
public class LoaderFrame extends Stage {
私有静态最终 LoaderFrame 实例 = new LoaderFrame();
public static LoaderFrame getInstance(){
return instance;
}
private Scene scene;
private AnchorPane root;
private BorderPane wraper;
private StackPane effectPane;
private JFXButton loaderPathButton;
public LoaderFrame(){
initScene();
this.initStyle(StageStyle.TRANSPARENT);
this.getIcons().add(new Image("file:imgs/favico/icon48.png"));
}
public void initScene(){
FXMLLoader loader = new FXMLLoader(Main.class.getResource("xml/loader_frame.fxml"));
root = null;
try {
root = loader.load();
wraper = (BorderPane) root.lookup("#rootPane");
loaderPathButton = (JFXButton) root.lookup("#loaderPathButton");
effectPane = (StackPane) root.lookup("#effectPane");
scene = new Scene(root);
scene.setFill(Color.TRANSPARENT);
scene.getStylesheets().add("file:css/loader.css");
this.setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
}
public Pane getEffectPane(){
return effectPane;
}
}
【问题讨论】: