Javafx ScrollPane无法更改背景

问题描述 投票:0回答:2

我试过用:

scrollPane.setStyle(".scroll-pane > .viewport { -fx-background-color: red; }");

但它不起作用。我很确定我做这个css的事情是错的。

java javafx
2个回答
0
投票

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

-1
投票

ScrollPane有一个AnchorPane作为他的孩子。您必须设置此特定AnchorPane的背景颜色。

© www.soinside.com 2019 - 2024. All rights reserved.