我试过用:
scrollPane.setStyle(".scroll-pane > .viewport { -fx-background-color: red; }");
但它不起作用。我很确定我做这个css的事情是错的。
Node的样式不能有选择器,只能有CSS规则。换句话说,setStyle(“ - fx-background-color:red;”)将是有效的(尽管它可能无法实现您正在寻求的效果)。
但是,您可以使用lookup从CSS选择器获取视口。
在实现舞台之前,ScrollPane没有其结构。一旦舞台出现,.viewport
后裔将在那里:
stage.setOnShown(e ->
scrollPane.lookup(".viewport").setStyle("-fx-background-color: red;"));
或者,您可以在单独的样式表中指定原始selcetor和规则:
.scroll-pane > .viewport { -fx-background-color: red; }
然后将其添加到Scene的样式表列表中:
scene.getStylesheets().add(
getClass().getResource("styles.css").toString());
ScrollPane有一个AnchorPane作为他的孩子。您必须设置此特定AnchorPane的背景颜色。