[英]How to change the background of a ScrollPane (JavaFX/ScalaFX)?
I want to change the background color of a ScrollPane. 我想更改ScrollPane的背景颜色。 This is part of my code where I try to do that:
这是我尝试执行的代码的一部分:
val sp=new javafx.scene.control.ScrollPane(new Group(new Text(...)))
sp.setPannable(true)
sp.setStyle("-fx-background-color: blue")
sp.setBackground(new Background(Array(new BackgroundFill(Color.DARKCYAN,new CornerRadii(0),Insets(0)))))
Text appears OK, but both attempts to change the background color have no effect, using: 文本显示为OK,但是使用以下方法更改背景颜色的两种尝试均无效:
Scala version 2.10.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_05). Scala版本2.10.3(Java HotSpot™64位服务器VM,Java 1.8.0_05)。
Inspecting with Scenic View , I discover that two StackPanes have unexpectedly appeared in the scene graph below the ScrollPane, so the hierarchy is: 使用Scenic View进行检查,我发现两个StackPanes意外出现在ScrollPane下面的场景图中,因此层次结构为:
If I change the background of either of the StackPane-s to, say, "-fx-background-color: blue"
(with Scenic View ), it has effect, but not the style of the ScrollPane. 如果我将两个StackPane-s的背景更改为
"-fx-background-color: blue"
(带有Scenic View ),则它具有效果,但没有ScrollPane的样式。 But how to do that from code? 但是如何从代码中做到这一点呢? If I do
如果我做
println(sp.content())
, it says , 它说
Group@567fa81a
集团@ 567fa81a
Is there a simple way to access the StackPanes or change the background? 有没有简单的方法来访问StackPanes或更改背景? I could "slap in" a big filled rectangle, but that seems ugly and complicates resizing, what is wrong with the background proper?
我可以“拍打”一个大的填充矩形,但这看起来很丑,并且使调整大小变得复杂,适当的背景怎么了?
Similar to JScrollPane
, JavaFX ScrollPane
has a StackPane
container within itself called viewport
. 与
JScrollPane
相似,JavaFX ScrollPane
在其内部具有一个称为viewport
的StackPane
容器。 So to set background for your ScrollPane
use this CSS rule (ie to set red background color): 因此,使用以下CSS规则为
ScrollPane
设置背景(即,设置红色背景色):
.scroll-pane .viewport {
-fx-background-color: red;
}
sp.setStyle("-fx-background: blue")
代替:
sp.setStyle("-fx-background-color: blue")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.