JavaFX ScrollPane怎样设置透明背景?

5

我无法在scrollpane中设置透明背景。可能是因为它包含anchorpane,而anchorpane包含按钮?来自scrollpane的白色背景和anchorpane的红色背景:

enter image description here

2个回答

11

您需要使用 CSS,并像这样:


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

由于ScrollPane具有Viewport,因此

.scroll-pane > .viewport {  //not .scrollpane but .scroll-pane
   -fx-background-color: transparent;
}

或者。
.scroll-pane .viewport {
       -fx-background-color: transparent;
}

如果它不起作用,那么你可能没有很好地定义外部CSS文件,或者你已经将某些类型的容器添加到了ScrollPane中,该容器还具有默认的背景颜色。

0

在FXML中为AnchorPane设置一个ID,然后在CSS中将该ID分配给-fx-background-color:transparent。

希望这个方法有效。

如果无效,请参考此问题:

JavaFX ScrollPane border and background


不起作用,我尝试了在scrollpane css中加入以下代码:.scrollpane > .viewport{ -fx-background-color: transparent; }但是没有任何效果。 - ground
已更新答案并附上链接,请参考该链接。 - Rahul Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接