gpt4 book ai didi

JavaFX FXML - 带有图标和文本的按钮

转载 作者:行者123 更新时间:2023-11-30 01:49:29 31 4
gpt4 key购买 nike

我想使用 FXML 添加带有图标和文本的按钮。

我知道在 FXML 中我可以使用以下代码添加图像:

<ImageView id="boxImage" fitWidth="100" fitHeight="100">
<image>
<Image url="@/com/example/app/resources/icons/office.png" />
</image>
</ImageView>

但我想知道如何将其与按钮代码合并:

<Button text="DATE"/>

非常欢迎任何帮助或示例,因为我完全不知道如何使用 FXML 应用图像。

编辑

我所做的以下回答:

<Button text="INSTRUMENT" styleClass="">
<graphic>
<ImageView pickOnBounds="true" preserveRatio="true">
<image>
<Image url="/com/example/app/resources/icons/instrument.png" />
</image>
</ImageView>
</graphic>
</Button>

但我得到:

enter image description here

该按钮增加了高度,因此不再与其他没有图像的按钮对齐。

最佳答案

你可以在 fxml 中做这样的事情

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

请参阅此处的示例 Styling a JavaFX 2 button using FXML only - How to add an image to a button?

更新:如果您使用 Java 8

float 宽度 = com.sun.javafx.tk.Toolkit.getToolkit().getFontLoader().computeStringWidth(btn.getText(), btn.getFont());

然后使用图像的宽度。

关于JavaFX FXML - 带有图标和文本的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56573887/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com