使用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)