css - 我无法在 JavaFX 中获得透明舞台

标签 css javafx background transparent stage

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

这是代码:

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);
    }

}

我做错了什么还是我真的无法获得透明背景?

这就是其他阶段加载时的样子,但我希望它即使在主阶段加载之前也能像这样工作,或者至少我想删除您可以在其中看到的灰色矩形另一个屏幕截图 This is what it looks like when also the other stage loads up

最佳答案

灰色背景是您的“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);
    }

}

关于css - 我无法在 JavaFX 中获得透明舞台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35668718/

相关文章:

jQuery使用Ajax填充选择列表导致宽度改变

html - <td> 中的表格每边被压扁 2px

java 和 javafx 集成程序

java - 在javaFx中,为什么在向窗口添加元素时为什么需要调用 “getChildren()”?

java - Java 8 中 JavaFx 中的按键事件问题

jquery - 如何设置姐妹或兄弟 div 的样式

javascript - 使用单个 HTML 文档添加多个页面

css - 背景图像未拉伸(stretch)

python - 如何使用蒙版去除python中的背景

jquery - 表格行悬停背景色圆 Angular 效果