Styling a JavaFX 2 button using FXML only - How to add an image to a button?

前端 未结 3 2141
臣服心动
臣服心动 2020-11-30 05:35

I want to change the styling of a button, most of the threads here and the articles on the internet show how to do it using Java code, which I don\'t see it as a real good s

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 06:11

    FXML is used just for designing the layout, for styling you can use css and reference it from your FXML file :

     
        
      
    

    To add an image to a fx:id="btn" button in your css:

    #btn {          
        -fx-background-image: url("Add.png");
        -fx-background-size: 18 18;
        -fx-background-repeat: no-repeat;
        -fx-background-position:left;   
    }
    

    This Github repository provide a complete running example:

    enter image description here

提交回复
热议问题