我正在构建一个涉及 JavaFX 中的 TextArea
和 TextField
的应用程序。我想包含使用 ColorPicker
更改字体颜色的功能。通过执行以下操作,我可以轻松自定义背景颜色
backgroundColorPicker.setOnAction(event -> {
Color color = backgroundColorPicker.getValue();
Background background = new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY));
Region region = ( Region ) console.lookup( ".content" );
region.setBackground(background);
input.setBackground(background);
});
如何更改字体颜色?到目前为止我只有
foregroundColorPicker.setOnAction(event -> {
Color color = foregroundColorPicker.getValue();
});
我一直无法找到更改字段字体颜色的方法。
最佳答案
我能够通过使用颜色值并将其转换为 CSS,然后将该 CSS 应用于字段来解决这个问题。
foregroundColorPicker.setOnAction(event -> {
Color color = foregroundColorPicker.getValue();
double red = color.getRed() * 255;
double green = color.getGreen() * 255;
double blue = color.getBlue() * 255;
double alpha = color.getOpacity() * 255;
String colorString = String.format("-fx-text-fill: rgba(%f,%f,%f,%f) ;", red, green, blue, alpha);
console.setStyle(colorString) ;
input.setStyle(colorString);
});
关于java - ColorPicker 中 TextField 中的字体颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45623854/