[英]JavaFX fade out stage and close
我很難發現這是否可能。 大多數人想要的常見行為是淡出node
的擴展,這完全可以通過FadeTransition
但是,我試圖淡出整個舞台,所以想象關閉正在運行的程序而不是簡單地殺死窗口(即顯示 - >不顯示),我希望窗口( stage
)在2秒內淡出祝酒或通知會。
使用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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.