如何设置弹出背景透明Java Fx

How to set pop up background Transparent Java Fx

我正在尝试显示来自我的 javaFx 应用程序的成功消息 gif。

public static void successGif() {
    int size = 400;
    ImageView splash = new ImageView(new Image("file:src/main/resources/img/success3.gif"));
    splash.setStyle("-fx-background-color: transparent;");
    splash.setFitWidth(size);
    splash.setFitHeight(size);
    splash.setPickOnBounds(true);
    Pane splashLayout = new Pane();
    splashLayout.getChildren().add(splash);
    final Stage initStage = new Stage();
    Scene successScene = new Scene(splashLayout, size, size);
    successScene.setFill(Color.TRANSPARENT);
    initStage.initStyle(StageStyle.TRANSPARENT);
    initStage.setWidth(size);
    initStage.setHeight(size);
    initStage.setScene(successScene);
    initStage.show();
    new Thread(() -> {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Platform.runLater(initStage::close);

    }).start();
}

以上函数可以成功创建gif弹窗。现在,我尝试通过应用程序中的按钮调用此函数,弹出窗口的背景仍然是白色。但是当我通过在应用程序的 start() 函数中调用它来测试相同的函数时,它按预期工作。我该如何解决这个问题?

我需要在我的应用程序中多次调用此函数。

如何使背景透明。

以下是gif,需要时可以试试,谢谢

您可以使用 javafx.scene.Group 而不是直接向场景添加启动布局, 您可以将 void 方法 successGif() 更改为

public static Stage successGif() {
    int size = 600;
    ImageView splash = new ImageView(new
    Image("file:src/main/resources/img/success3.gif"));
    splash.setStyle("-fx-background-color: transparent;");
    splash.setFitWidth(size);
    splash.setFitHeight(size);
    splash.setPickOnBounds(true);
    Pane splashLayout = new Pane();
    splashLayout.getChildren().add(splash);
    final Stage initStage = new Stage();
    Group group = new Group();
    group.getChildren().add(splashLayout);
    //        group.setStyle("-fx-background-color: transparent");
    Scene successScene = new Scene(group, size, size);
    successScene.setFill(Color.TRANSPARENT);
    initStage.initStyle(StageStyle.TRANSPARENT);
    initStage.setWidth(size);
    initStage.setHeight(size);
    initStage.setScene(successScene);
    initStage.setAlwaysOnTop(true);
    initStage.show();
    return initStage;
}

然后如果你想要 Stage 对象,那么你可以从方法中获取返回的对象