2016-02-24 70 views
4

我在按钮中使用SVG作为图像。但是我无法通过CSS为它填充颜色。 以下是渲染按钮的代码。如何使用javaFX中的CSS设计SVG样式FXML

<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; 
} 

回答

1

这里是它如何工作的。 我必须设置按钮的样式,并使用该类来设置按钮中的svg样式。

<Button onAction="#closeApplication" styleClass="closeButton"> 
     <graphic> 
      <SVGPath content="M10,16 10,0 0,8z" /> 
     </graphic> 
</Button> 

这里是CSS

.closeButton{ 

} 
.closeButton SVGPath{ 
    -fx-fill: red; 
} 
0

我知道这是一个老问题,但这种解决方案OP是不是最好的之一,我的脑海里。如果您遇到同样的问题,我的建议是阅读JavaFX CSS Reference Guide (JFX 8),尤其是重定向到selectors link

与最初的代码在这里最简单的办法是以下几点:

<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; 
}