【问题标题】:How to use JavaFX FadeTransition In and Out如何使用 JavaFX 淡入淡出淡入淡出
【发布时间】: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


    【解决方案1】:

    您在loadScene 中再次使用fadeOut。我认为你想在那里使用fadeIn?此外,当newRoot 甚至还不是场景图形的一部分时,您正在启动动画。您可能想先尝试设置场景(但以不透明度 0 开始关闭 newRoot)然后启动动画。

    作为一般说明,我还建议您在更改屏幕时不要交换场景(甚至场景的根),而只是在 GUI 中交换窗格。这也将为您提供更多的动画灵活性。例如,您可以同时淡入新窗格并淡出旧窗格,这在使用场景时是不可能的。

    【讨论】:

    • 你所说的交换窗格很有趣。但我有一个问题。如果我为此使用窗格,那么所有这些窗格都应该在同一个 .fxml 中,这会导致所有窗格都在同一个控制器中吗?如果答案是肯定的,我认为这将很难合作。
    • 没必要。每个屏幕都可以有一个单独的 FXML 和控制器。您可以根据需要简单地加载它们。我还建议使用fx:root 构造。它可以很好地封装。有关fx:root 的更多信息,请参阅my answer here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    相关资源
    最近更新 更多