我无法在 JavaFX 中获得透明阶段

I can't get a transparent stage in JavaFX

这是我的代码,我试图在主舞台开始之前加载具有透明背景的初始屏幕图像。它们几乎同时出现,但最大的问题是我先得到一个灰色矩形:.

代码如下:

public class Menu extends Application {

    private Pane splashLayout;
    private Stage mainStage;
    private ImageView splash;

    // Creating a static root to pass to ScreenControl
    private static BorderPane root = new BorderPane();

    public void start(Stage splashStage) throws IOException {

        final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.splash = new ImageView(new Image(getClass().getResource("/splash.png").toString()));

        splashStage.initStyle(StageStyle.TRANSPARENT);      
        showSplash(splashStage, screenSize);

        // Constructing our scene using the static root
        root.setCenter(new ScrollPane);
        Scene scene = new Scene(root, screenSize.getWidth(), screenSize.getHeight());
        showMainStage(scene);

        if (splashStage.isShowing()) {
            mainStage.setIconified(false);
            splashStage.toFront();
            FadeTransition fadeSplash = new FadeTransition(Duration.seconds(1.5), splashLayout);
            fadeSplash.setDelay(Duration.seconds(3.5));
            fadeSplash.setFromValue(1.0);
            fadeSplash.setToValue(0.0);
            fadeSplash.setOnFinished(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent) {
                    splashStage.hide();
                }
            });
            fadeSplash.play();
        }
    }

    private void showMainStage(Scene scene) {
        mainStage = new Stage(StageStyle.DECORATED);    
        mainStage.setTitle("book-depot");
        mainStage.getIcons().add(new Image(getClass().getResourceAsStream("/icon.png")));
        mainStage.setScene(scene);
        mainStage.show();
    }

    private void showSplash(Stage splashStage, Dimension screenSize) {
        splashLayout = new StackPane();
        splashLayout.setStyle("-fx-background-color: transparent;");
        splashLayout.getChildren().add(splash);
        Scene splashScene = new Scene(splashLayout, 690, 590);
        splashScene.setFill(Color.TRANSPARENT);
        splashStage.setScene(splashScene);
        splashStage.show();
    }

    public void mainGui(String[] args) {
        launch(args);
    }

}

是我做错了什么还是真的弄不到透明背景?

这是其他阶段加载时的样子,但我希望它甚至在主阶段加载之前也能像这样工作,或者至少我想删除灰色矩形,你可以在另一个屏幕截图中看到

灰色背景是您的"mainStage",因为您同时显示初始阶段和主要阶段。在开始显示飞溅阶段时,您可以只初始化(不显示)主舞台并在动画结束后显示它:

public class ModifiedMenu extends Application
{

    private Pane splashLayout;
    private Stage mainStage;
    private ImageView splash;

    // Creating a static root to pass to ScreenControl
    private static BorderPane root = new BorderPane();

    public void start(Stage splashStage) throws IOException {

        final Dimension2D screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.splash = new ImageView(new Image(getClass().getResource("/splash.png").toString()));

        splashStage.initStyle(StageStyle.TRANSPARENT);      
        showSplash(splashStage, screenSize);

        // Constructing our scene using the static root
        root.setCenter(new ScrollPane());
        Scene scene = new Scene(root, screenSize.getWidth(), screenSize.getHeight());
        initMainStage(scene);

        if (splashStage.isShowing()) {
            splashStage.toFront();
            FadeTransition fadeSplash = new FadeTransition(Duration.seconds(1.5), splashLayout);
            fadeSplash.setDelay(Duration.seconds(3.5));
            fadeSplash.setFromValue(1.0);
            fadeSplash.setToValue(0.0);
            fadeSplash.setOnFinished(new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent actionEvent) {
                    splashStage.hide();
                    mainStage.show();
                }
            });
            fadeSplash.play();
        }
    }

    private void initMainStage(Scene scene) {
        mainStage = new Stage(StageStyle.DECORATED);    
        mainStage.setTitle("book-depot");
        mainStage.getIcons().add(new Image(getClass().getResourceAsStream("/icon.png")));
        mainStage.setScene(scene);
    }

    private void showSplash(Stage splashStage, Dimension2D screenSize) {
        splashLayout = new StackPane();
        splashLayout.setStyle("-fx-background-color: transparent;");
        splashLayout.getChildren().add(splash);
        Scene splashScene = new Scene(splashLayout, 690, 590);
        splashScene.setFill(Color.TRANSPARENT);
        splashStage.setScene(splashScene);
        splashStage.show();
    }

    public void mainGui(String[] args) {
        launch(args);
    }

}