Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Css 单击复选框更改代码区文本背景_Css_Checkbox_Javafx_Highlight_Richtextfx - Fatal编程技术网

Css 单击复选框更改代码区文本背景

Css 单击复选框更改代码区文本背景,css,checkbox,javafx,highlight,richtextfx,Css,Checkbox,Javafx,Highlight,Richtextfx,使用我已经能够创建一个CodeArea,它接受一个字符串,并突出显示属于string[]KEYWORDS数组的任何单词 我在code区域上方有一个复选框控件,我在控制器代码中将handler()附加到该控件,因此每当单击该复选框时,它都会取消突出显示code区域中所有突出显示的单词。我无法取消突出显示(code区域中的文本)(使文本背景为白色)因为styleClass是在下面显示的StyleSpans方法中定义的。现在一切正常,除了单击复选框,它不会取消突出显示以下文字: FXML: <H

使用我已经能够创建一个
CodeArea
,它接受一个字符串,并突出显示属于
string[]KEYWORDS
数组的任何单词

我在
code区域
上方有一个复选框控件,我在控制器代码中将
handler()
附加到该控件,因此每当单击该复选框时,它都会取消突出显示
code区域
中所有突出显示的单词。我无法取消突出显示(
code区域
中的文本)(使文本背景为白色)因为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>
@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();
}
.highlightKeyWords{
-fx-font-size:13px;
-fx-background-color: grey;
-fx-background-fill: yellow;
}
这里的
-fx背景填充:黄色可以很好地高亮显示,但如何在处理程序中将其更改为白色?

使用在
代码区定义的“查找颜色”,并使用
设置样式更改它

首先,只需将CSS id添加到
代码区

 <CodeArea id="codeBox" fx:id="codeBox" wrapText="true" prefHeight="240.0"    prefWidth="339.0">
 </CodeArea>
然后在
initialize()
方法中

highlightBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
    if (isNowSelected) {
        codeBox.setStyle("-keyword-highlight: yellow ;");
    } else {
        codeBox.setStyle("-keyword-highlight: white ;");
    }
});
使用
code区域中定义的“查找颜色”,并使用
setStyle
对其进行更改

首先,只需将CSS id添加到
代码区

 <CodeArea id="codeBox" fx:id="codeBox" wrapText="true" prefHeight="240.0"    prefWidth="339.0">
 </CodeArea>
然后在
initialize()
方法中

highlightBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
    if (isNowSelected) {
        codeBox.setStyle("-keyword-highlight: yellow ;");
    } else {
        codeBox.setStyle("-keyword-highlight: white ;");
    }
});

对詹姆斯,你很棒。我也学到了很多!!导致错误的唯一原因是它应该是
highlightBox.selectedProperty()
,并且
if
语句缺少其结尾
}
,但其他一切都可以正常工作,并且都有意义。再次感谢,是的!詹姆斯,你很棒。我也学到了很多!!导致错误的唯一原因是它应该是
highlightBox.selectedProperty()
,并且
if
语句缺少其结尾
}
,但其他一切都可以正常工作,并且都有意义。再次感谢。