我想让用户能够通过“设置”菜单编辑应用 UI 颜色。 默认颜色在 .css 文件中定义,例如:
.vertex {
-fx-fill: rgba(64, 196, 255, 1);
}
在上面的示例中,我想将 -fx-fill color 覆盖为用户将从颜色拾取对话框中选择的任何颜色。是否可以在运行时更新 CSS 类属性?怎么样?
最佳答案
您可以在 CSS 文件中使用查找颜色:
.vertex {
-vertex-fill: rgba(64, 196, 255, 1);
-fx-fill: -vertex-fill ;
}
然后您可以在运行时使用内联样式对其进行修改。您可以对组件的任何祖先调用 setStyle()
,它将应用于所有后代组件。例如。要将更改应用于样式类 .vertex
的场景中的所有内容,请设置场景根的样式:
Color selectedColor = colorPicker.getValue();
int red = (int) (255 * selectedColor.getRed());
int green = (int) (255 * selectedColor.getGreen());
int blue = (int) (255 * selectedColor.getBlue());
double opacity = selectedColor.getOpacity();
String userColor = String.format("rgba(%d, %d, %d, %f)", red, green, blue, opacity) ;
scene.getRoot().setStyle("-vertex-fill: " + userColor + " ;");
关于css - 如何在运行时覆盖(编辑)CSS 颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43455083/