简体   繁体   English

具有透明ScrollPane的透明舞台不是透明的

[英]Transparent Stage with transparent ScrollPane is not transparent

Here I have a method which generates a Stage that should be transparent with a ScrollPane of a bunch of rectangles. 在这里,我有一个生成舞台的方法,该舞台应该与一堆矩形的ScrollPane透明。

private Stage createStage()
    {
        Stage stage = new Stage(StageStyle.TRANSPARENT);

        FlowPane flw = new FlowPane();
        flw.setBackground(Background.EMPTY);

        Random gen = new Random(5);
        for (int i = 0; i < 50; i++)
        {
            int size = 50 + gen.nextInt(50);
            flw.getChildren().add(new Rectangle(size, size, Color.web("rgb(" + gen.nextInt(255) + "," + gen.nextInt(255) + "," + gen.nextInt(255) + ")")));
        }

        ScrollPane root = new ScrollPane(flw);
        root.setMaxSize(500, 500);
        root.setBackground(Background.EMPTY);

        Scene scene = new Scene(root, Color.TRANSPARENT);
        stage.setScene(scene);

        return stage;
    }

This is the output of the code above when Stage.show() is called: 这是上面调用Stage.show()时的代码的输出:

在此处输入图片说明

This stage pops up in a new window but as you see, the background is white and cannot be seen through. 该阶段会在新窗口中弹出,但是如您所见,背景为白色,无法透视。 I have a feeling the problem the ScrollPane or the FlowPane, because just having StageStyle.TRANSPARENT and a single Square made the square pop up without any white background. 我有一个ScrollPane或FlowPane的问题,因为仅具有StageStyle.TRANSPARENT和一个Square即可使正方形弹出而没有任何白色背景。

What am I doing wrong? 我究竟做错了什么? I've already tried doing: 我已经尝试做过:

Pane.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));

It's due to the viewport of your ScrollPane not receiving the transparent background 这是由于ScrollPane的视口未接收到透明背景

If you apply the following css: 如果应用以下css:

.scroll-pane > .viewport {
   -fx-background-color: transparent;
}

on top of the code you provided, you get the below: 在您提供的代码之上,您将获得以下内容:

在此处输入图片说明


You will need to add a style sheet to your project and the scene: 您将需要在项目和场景中添加样式表:

Eg If the style sheet is in the src directory 例如,如果样式表位于src目录中

scene.getStylesheets().add("CSS/stylesheet.css");

I prefer to add a CSS directory to allow for more specific style sheets later: 我更喜欢添加一个CSS目录,以便以后可以使用更多特定的样式表:

 scene.getStylesheets().add("CSS/stylesheet.css"); 

Don't forget that if you're launching from a jar, you will need to use toExternalForm() 不要忘记,如果从jar启动,则需要使用toExternalForm()

With the style sheet added, you can reduce some Java lines of code used for styling by implementing them with css instead, which should make your code easier to read 添加样式表后,您可以通过使用CSS来实现,从而减少用于样式设计的某些Java代码行,这应使您的代码更易于阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM