我正在使用SVG作为按钮中的图像。但是我无法通过CSS填充颜色。
下面是呈现按钮的代码:
<Button onAction="#closeApplication" >
<graphic>
<SVGPath content="M10,16 10,0 0,8z" styleClass="button‐icon‐shape" />
</graphic>
</Button>
这里是CSS。
.button-icon-shape SVGPath{
-fx-fill: red;
}
我知道这是一个老问题,但是这个原始的解决方案并不是最好的。如果你遇到了同样的问题,我的建议是阅读JavaFX CSS参考指南(JFX 8),特别是重定向到这个选择器链接。
最简单的解决方案是使用初始代码:
<Button onAction="#closeApplication">
<graphic>
<SVGPath content="M10,16 10,0 0,8z" styleClass="button-icon-shape" />
</graphic>
</Button>
JavaFX相关的CSS应该是:
.button-icon-shape {
-fx-fill:red;
}
这是它的工作方式。 我需要为按钮设置样式,并使用该类来为按钮中的svg设置样式。
<Button onAction="#closeApplication" styleClass="closeButton">
<graphic>
<SVGPath content="M10,16 10,0 0,8z" />
</graphic>
</Button>
这是CSS代码
.closeButton{
}
.closeButton SVGPath{
-fx-fill: red;
}