【发布时间】:2015-05-27 02:26:33
【问题描述】:
我正在尝试通过“淡出”和“淡入”过渡来改变我的舞台场景。我真的很想得到它,但根本没有。
第一次,它不起作用。然后,有时它可以正常工作,而其他新场景会在几毫秒内完全显示出来,然后继续进行其余的过渡。唯一运行完美的是 FadeOut。
我认为我做错了什么,但我不知道是什么。 我尝试过使用 FadeTransition 和 Timeline,但结果始终相同。
我向你展示我正在工作的代码片段:
//当我想改变场景时从控制器调用
private static FadeTransition fadeOut = new FadeTransition();
private static FadeTransition fadeIn = new FadeTransition();
public void setScene(final String resource_fxml, final String title) {
fadeOut.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
loadScene(resource_fxml, title);
}
});
fadeOut.setNode(lastRoot);
fadeOut.setDuration(Duration.millis(Config.TRANSITIONS_TIME));
fadeOut.setFromValue(1.0);
fadeOut.setToValue(0.0);
fadeOut.play();
}
private void loadScene(String resource_fxml, String title) {
double width = SceneManager.lastScene.getWidth();
double height = SceneManager.lastScene.getHeight();
Parent newRoot = null;
try {
newRoot = FXMLLoader.load(getClass().getResource(resource_fxml));
} catch (IOException ex) {
log.error("Resource not found");
}
// DoubleProperty opacity = newRoot.opacityProperty();
// Timeline fadeIn = new Timeline(
// new KeyFrame(Duration.ZERO, new KeyValue(opacity, 0.0)),
// new KeyFrame(Duration.millis(Config.TRANSITIONS_TIME), new KeyValue(opacity, 1.0))
// );
// fadeIn.play();
fadeOut.setNode(newRoot);
fadeOut.setDuration(Duration.millis(Config.TRANSITIONS_TIME));
fadeOut.setFromValue(0.0);
fadeOut.setToValue(1.0);
fadeOut.play();
lastScene = new Scene(newRoot, width, height);
mainStage.setTitle(title);
mainStage.setScene(lastScene);
mainStage.show();
lastRoot = newRoot;
}
编辑:我展示了运行良好的最终代码 ("loadScene")。感谢您的提示,Steven Van Impe!
private void loadScene(String resource_fxml, String title) {
double width = SceneManager.lastScene.getWidth();
double height = SceneManager.lastScene.getHeight();
Parent newRoot = null;
try {
newRoot = XMLLoader.load(getClass().getResource(resource_fxml));
} catch (IOException ex) {
log.error("Resource not found");
}
fadeIn.setNode(newRoot);
fadeIn.setDuration(Duration.millis(Config.TRANSITIONS_TIME));
fadeIn.setFromValue(0.0);
fadeIn.setToValue(1.0);
DoubleProperty opacity = newRoot.opacityProperty();
opacity.set(0);
lastScene = new Scene(newRoot, width, height);
mainStage.setTitle(title);
mainStage.setScene(lastScene);
fadeIn.play();
lastRoot = newRoot;
}
【问题讨论】:
标签: javafx fadein fadeout scene stage