JavaFX TextFlow设置默认文本颜色
作为标题,是否可以将默认颜色应用于TextFlow组件的所有文本?
As the title, it's possible to to apply a default color to all text of a TextFlow component?
TextFlow textFlow = new TextFlow();
textFlow.setId("supertextflow");
// Somewhere else in the code
textFlow.getChildren()
.add(new Text("Dynamic added text! OMG!"));
我尝试了不同的解决方案,但没有一个起作用
I tryed different solution but none of them works
#supertextflow {
-fx-text-fill: red;
}
#supertextflow * .text{
-fx-fill: red;
}
#supertextflow > * > .text {
-fx-fill: red;
}
我知道Text是另一个组件,但是为什么我不能从其父级设置样式?
I know that Text is another component, but why i can't style it from it's parent?
Well you can't do that with Text
cause it's style class doesn't have fill css rule if you look at the JavaFX CSS Reference Guide. So I would suggest to leave the Text
and use Label
instead. If you do then you could use the css rule below :
#supertextflow > .label {
-fx-text-fill: blue;
-fx-font-size : 20px;
}
如果要继续使用Text,则必须在FlowPane中为每个元素(Text)设置一个特定的ID(例如#customText),然后使用它来设置CSS规则,如下所示:
In case you want to keep using Text you will have to set each element (Text) inside the FlowPane a specific id (ex. #customText) and then use it to set the CSS rule like below :
#supertextflow > #customText {
-fx-fill: red;
-fx-font-size : 20px;
}
如下面的建议中所述,James_D应该在CSS规则上使用 Type Selector (我猜这是正确的术语),以便为所有样式设置样式TextFlow
中的文本节点,无需在其上设置任何ID:
Edit : As James_D mentioned on the commends below you should use Type Selector (I am guessing that's the correct term) on the CSS rule in order to style all the Text nodes inside your TextFlow
without needed to set any ids on them :
#supertextflow > Text {
-fx-fill: red;
-fx-font-size : 20px;
}