JavaFX淡出阶段并关闭

Jas*_* M. 4 java windows fade toast javafx-8

我很难发现这是否可能.大多数人想要的常见行为是淡出a的扩展node,这完全可以通过aFadeTransition

但是,我试图淡出整个舞台,所以想象关闭正在运行的程序,而不是简单地杀死窗口(即显示 - >不显示),我希望窗口(stage)在2秒内淡出,就像一个吐司或通知会.

Cam*_*uxe 7

使用KeyFrame创建时间轴,以更改舞台场景根节点的不透明度.还要确保将舞台样式和场景填充设置为透明.然后在时间线完成后退出程序.

下面是一个带有单个大按钮的应用程序,单击此按钮将需要2秒才能消失,然后程序将关闭.

public class StageFadeExample extends Application {

    @Override
    public void start(Stage arg0) throws Exception {
        Stage stage = new Stage();
        stage.initStyle(StageStyle.TRANSPARENT); //Removes window decorations

        Button close = new Button("Fade away");
        close.setOnAction((actionEvent) ->  {
            Timeline timeline = new Timeline();
            KeyFrame key = new KeyFrame(Duration.millis(2000),
                           new KeyValue (stage.getScene().getRoot().opacityProperty(), 0)); 
            timeline.getKeyFrames().add(key);   
            timeline.setOnFinished((ae) -> System.exit(1)); 
            timeline.play();
        });

        Scene scene = new Scene(close, 300, 300);
        scene.setFill(Color.TRANSPARENT); //Makes scene background transparent
        stage.setScene(scene);
        stage.show();
    }

    public static void main (String[] args) {
        launch();
    }
}
Run Code Online (Sandbox Code Playgroud)