我正在尝试为整个段落设置文本(字体)颜色,而不仅仅是单个段。为了做到这一点,我修改了Rich Text FX 演示作为一个实验:在演示中,更改整个段落的背景颜色可以正常工作。因此,我将更改背景颜色的代码替换为更改ParStyle.java中的前景色,方法如下:
为清楚起见,将成员 backgroundColor 重命名为 textColor :
final Optional<TextAlignment> alignment;
final Optional<Color> textColor; // changed from background color to text color
final Optional<Indent> indent;
final int foldCount;
并且还改变了toCSS中css String的对应写法(接替TextStyle cssString):
public String toCss() {
StringBuilder sb = new StringBuilder();
alignment.ifPresent(al -> {
String cssAlignment;
switch(al) {
case LEFT: cssAlignment = "left"; break;
case CENTER: cssAlignment = "center"; break;
case RIGHT: cssAlignment = "right"; break;
case JUSTIFY: cssAlignment = "justify"; break;
default: throw new AssertionError("unreachable code");
}
sb.append("-fx-text-alignment: " + cssAlignment + ";");
});
textColor.ifPresent(color -> {
//sb.append("-fx-background-color: " + TextStyle.cssColor(color) + ";");
sb.append( "-fx-fill: " + TextStyle.cssColor(color) + ";");
});
if ( foldCount > 0 ) sb.append( "visibility: collapse;" );
return sb.toString();
}
但是在它使用背景颜色之前,它对文本颜色没有影响。这是一个普遍的问题还是我对 CSS 字符串做错了什么?