更改 JavaFX textarea 中选择的前景颜色
Change foreground color of selection in JavaFX textarea
我想更改 JavaFx 文本区域中选定文本的样式。我已经通过设置 -fx-accent
成功更改了背景颜色,但是我没有找到如何更改文本前景色的方法。有谁知道如何实现这一目标?我已经通过 modena.css 文件并尝试了很多属性,但直到现在都没有成功。
非常感谢!
根据JavaFX 8 CSS reference documentation,文本输入控件(如文本区域)中选中文本的前景填充的css属性,好像是:
-fx-highlight-text-fill
样本
左边是没有焦点但有一些选定文本的 TextArea。右侧是一个 TextArea,它具有焦点和一些选定的文本。自定义样式应用于选定的文本前景和背景,颜色因焦点状态而异。
正文-highlighter.css
.text-input {
-fx-highlight-fill: paleturquoise;
-fx-highlight-text-fill: blue;
}
.text-input:focused {
-fx-highlight-fill: palegreen;
-fx-highlight-text-fill: fuchsia;
}
TextHighlighter.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextHighlighter extends Application {
@Override
public void start(Stage stage) throws Exception {
TextArea textArea = new TextArea(
"The quick brown cat ran away with the spoon."
);
textArea.selectRange(4, 9);
textArea.setWrapText(true);
VBox layout = new VBox(10, new Button("Button"), textArea);
final Scene scene = new Scene(layout);
scene.getStylesheets().add(
this.getClass().getResource("text-highlighter.css").toExternalForm()
);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我想更改 JavaFx 文本区域中选定文本的样式。我已经通过设置 -fx-accent
成功更改了背景颜色,但是我没有找到如何更改文本前景色的方法。有谁知道如何实现这一目标?我已经通过 modena.css 文件并尝试了很多属性,但直到现在都没有成功。
非常感谢!
根据JavaFX 8 CSS reference documentation,文本输入控件(如文本区域)中选中文本的前景填充的css属性,好像是:
-fx-highlight-text-fill
样本
左边是没有焦点但有一些选定文本的 TextArea。右侧是一个 TextArea,它具有焦点和一些选定的文本。自定义样式应用于选定的文本前景和背景,颜色因焦点状态而异。
正文-highlighter.css
.text-input {
-fx-highlight-fill: paleturquoise;
-fx-highlight-text-fill: blue;
}
.text-input:focused {
-fx-highlight-fill: palegreen;
-fx-highlight-text-fill: fuchsia;
}
TextHighlighter.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TextHighlighter extends Application {
@Override
public void start(Stage stage) throws Exception {
TextArea textArea = new TextArea(
"The quick brown cat ran away with the spoon."
);
textArea.selectRange(4, 9);
textArea.setWrapText(true);
VBox layout = new VBox(10, new Button("Button"), textArea);
final Scene scene = new Scene(layout);
scene.getStylesheets().add(
this.getClass().getResource("text-highlighter.css").toExternalForm()
);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}