将图标设置为JFXButton

时间:2019-08-10 14:54:52

标签: javafx

如何从控制器类向icon添加fontawesome iconJFXButton

public void initialize() {
    JFXButton hamburger = new JFXButton();
} 

2 个答案:

答案 0 :(得分:2)

尝试一下,它可能会起作用-

JFXButton hamburger = new JFXButton();
Image image = new Image(getClass().getResourceAsStream("icon.png"));
hamburger.setGraphic(new ImageView(image));

答案 1 :(得分:1)

扩展@Mayur Patel答案,将JFXButton构造函数与文本和图形一起使用,请参见文档JFXButton

public JFXButton(String text, Node graphic) {
    super(text, graphic);
    initialize();
}

因此,在您的情况下,它应如下所示:

Image image = new Image(getClass().getResourceAsStream("icon.png"));
JFXButton hamburger = new JFXButton("Try me",image);

带有带字体的图标(如果您正在使用场景构建器,请记住首先将其导入到场景构建器中)

 <JFXButton fx:id="delete_btn" text="Try me">
   <graphic>
     <FontAwesomeIconView fill="WHITE" glyphName="play" size="16.0"/>
   </graphic>
 </JFXButton>