简体   繁体   中英

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

我想改变一个按钮的样式,这里的大多数线程和互联网上的文章都展示了如何使用Java代码来实现它,我认为这不是一个真正好的解决方案,有没有办法比如说通过仅使用FXML(无Css)设置一个带有一些文本和图像的按钮?

Solution using only fxml

As tarrsalah points out, css is the recommended way of doing this, though you can also do it in fxml if you prefer:

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml">
  <children>
    <Button layoutX="104.0" layoutY="81.0" mnemonicParsing="false" text="Love&#10;Potion">
      <font>
        <Font size="30.0" />
      </font>
      <graphic>
        <ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true">
          <image>
            <Image url="http://icons.iconarchive.com/icons/mirella-gabriele/fantasy-mediaeval/128/Poison-red-icon.png" />
          </image>
        </ImageView>
      </graphic>
    </Button>
  </children>
</AnchorPane>

To get the above in SceneBuilder , drag an ImageView on top of a Button and it will automatically be set as a graphic for the button. Then select the ImageView and type the url of the image into the ImageView's image field in the SceneBuilder properties pane.

Open the above fxml in SceneBuilder to see the image below.

爱情魔药


Alternate css attributes

Alternate css to the -fx-background* attributes.

  • -fx-graphic
  • -fx-padding
  • -fx-content-display
  • -fx-graphic-text-gap

These are just different, not necessarily better for what you are trying to do. It is just a preference thing as to which to use. I find these settings easier to use than the -fx-background* settings. They are more restrictive, but the syntax and options are much easier for me to understand and their meanings map to the JavaDoc API for Labeled .

A detailed description of the attributes is in the css reference guide .

Here's a example with a style setting the graphic embedded in the fxml, though it is always better to separate the style information out into a separate css stylesheet as in tarrsalah's sample.

<Button layoutX="138.0" layoutY="226.0" mnemonicParsing="false" style="-fx-graphic: url('http://icons.iconarchive.com/icons/mirella-gabriele/fantasy-mediaeval/128/Poison-red-icon.png')" text="Love&#10;Potion">
  <font>
    <Font size="20.0" />
  </font>
</Button>

Related solutions for adding images to buttons using only Java code

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

 <stylesheets>
    <URL value="@main.css" />
  </stylesheets>

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:

在此输入图像描述

A couple of changes I made to the css example (ref. #btn { in comment #3):

-fx-background-size: 100% 100%;
-fx-background-color: transparent;  

This removed the button outline and left only the image. I was able to resize the button selector in the Scene Builder like an ImageView and see the picture change in realtime. I added a ToolTip to explain the image since there was no text and no button outline. I plan to use techniques explained elsewhere and alter the image when clicked to give more visual feedback that the image is a button.

Also note: I needed to specify the button id in the 'CSS Id' (Scene Builder) instead of the 'fx:id' to get them to be linked.

Adding these changes to what was explained above allowed me to accomplish my goal: have a button that looks only like the image (eg round shape in squarish button).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM