2016-02-24 68 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; 
}