使用 RichTextFX我已经能够创建一个 CodeArea
,它接受一个字符串,并突出显示属于 String[] KEYWORDS
数组的所有单词。
我在 CodeArea
上方有一个复选框控件,我将 handler()
附加到我的 Controller 代码中,因此每当单击该复选框时,它都会取消突出显示所有CodeArea
中突出显示的单词。我无法取消突出显示(使文本背景变白)CodeArea
中的文本,因为 styleClass 是在如下所示的 StyleSpans
方法中定义的。现在一切正常,除了当我点击复选框时,它不会取消突出显示这些词:
FXML:
<HBox>
<CheckBox fx:id="highlightBox" mnemonicParsing="false" prefHeight="25.0" prefWidth="154.0" stylesheets="@styleMain.css" text="Highlight Words" />
</HBox>
<HBox>
<children>
<CodeArea fx:id="codeBox" wrapText="true" prefHeight="240.0" prefWidth="339.0">
</CodeArea>
</children>
</HBox>
Java Controller :
@FXML
CheckBox highlightBox;
@FXML
public CodeArea codeBox;
private static final String[] KEYWORDS = new String[] {
"one",
"two",
"three"
};
private static final String KEYWORD_PATTERN = "\\b(" + String.join("|", KEYWORDS) + ")\\b";
private static final Pattern PATTERN = Pattern.compile("(?<KEYWORD>" + KEYWORD_PATTERN + ")");
public static final String demoText = "one two three four five";
@Override
public void initialize(URL location, ResourceBundle resources) {
//Highlighting Words
codeBox.richChanges().subscribe(change -> {
codeBox.setStyleSpans(0, computeHighlighting(codeBox.getText()));
});
codeBox.replaceText(0, 0, demoText);
highlightBox.setOnAction(new EventHandler < ActionEvent > () {
@Override
public void handle(ActionEvent event) {
codeBox.setStyle("-fx-background-color: blue"); // this should instead change 'highlightKeyWords' background color to white
}
});
...
} //End of initialize
public static StyleSpans < Collection < String >> computeHighlighting(String text) {
Matcher matcher = PATTERN.matcher(text);
int lastKwEnd = 0;
StyleSpansBuilder < Collection < String >> spansBuilder = new StyleSpansBuilder < > ();
while (matcher.find()) {
String styleClass =
matcher.group("KEYWORD") != null ? "highlightKeyWords" :
null; /* never happens */
assert styleClass != null;
spansBuilder.add(Collections.emptyList(), matcher.start() - lastKwEnd);
spansBuilder.add(Collections.singleton(styleClass), matcher.end() - matcher.start());
lastKwEnd = matcher.end();
}
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd);
return spansBuilder.create();
}
在处理程序中,我成功地将 CodeArea
的背景更改为蓝色,但如果我改为尝试 propertyOverview.setStyle("-fx-background-fill: blue;");
它没有改变任何东西,单词保持突出显示。这是因为我应该引用在 computeHighlighting
方法中创建的 highlightKeyWords
。我应该使用类似 highlightKeyWords.setStyle("-fx-background-fill: white")
的东西吗?
CSS:
.highlightKeyWords{
-fx-font-size:13px;
-fx-background-color: grey;
-fx-background-fill: yellow;
}
此处 -fx-background-fill: yellow;
可以很好地突出显示,但如何在处理程序中将其更改为白色?
最佳答案
使用在 CodeArea
上定义的“查找颜色”,并使用 setStyle
更改它。
首先,只需将 CSS id 添加到 CodeArea
:
<CodeArea id="codeBox" fx:id="codeBox" wrapText="true" prefHeight="240.0" prefWidth="339.0">
</CodeArea>
然后按如下方式更新 CSS:
#codeBox {
-keyword-highlight: yellow ;
}
.highlightKeyWords{
-fx-font-size:13px;
-fx-background-color: grey;
-fx-background-fill: -keyword-highlight;
}
然后在您的initialize()
方法中执行
highlightBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
codeBox.setStyle("-keyword-highlight: yellow ;");
} else {
codeBox.setStyle("-keyword-highlight: white ;");
}
});
关于css - 单击复选框更改 CodeArea 文本背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35005883/