仅使用 FXML 为 JavaFX 2 按钮设置样式 - 如何向按钮添加图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16340269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Styling a JavaFX 2 button using FXML only - How to add an image to a button?
提问by AymenDaoudi
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 solution, is there any way for example to set a button with some text and an image inside it all by using FXML only (no Css) ?
我想改变一个按钮的样式,这里的大多数线程和互联网上的文章都展示了如何使用 Java 代码来做到这一点,我不认为它是一个真正好的解决方案,有什么方法可以例如仅使用 FXML(无 Css)设置一个带有一些文本和图像的按钮?
采纳答案by jewelsea
Solution using only fxml
仅使用 fxml 的解决方案
As tarrsalah points out, css is the recommended way of doing this, though you can also do it in fxml if you prefer:
正如 tarrsalah 指出的那样,推荐使用 css 来执行此操作,但如果您愿意,也可以在 fxml 中执行此操作:
<?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 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.
要在SceneBuilder 中获得上述内容,请将ImageView
a拖到 a之上,Button
它将自动设置为按钮的图形。然后ImageView
在 SceneBuilder 属性窗格中的 ImageView 的图像字段中选择图像的 url 并将其键入。
Open the above fxml in SceneBuilder to see the image below.
在 SceneBuilder 中打开上面的 fxml 以查看下图。
Alternate css attributes
替代 css 属性
Alternate css to the -fx-background*
attributes.
将 css 替换为-fx-background*
属性。
-fx-graphic
-fx-padding
-fx-content-display
-fx-graphic-text-gap
-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.
这些只是不同的,不一定更适合您尝试做的事情。至于使用哪个,这只是一种偏好。我发现这些设置比设置更容易使用-fx-background*
。它们更具限制性,但语法和选项对我来说更容易理解,并且它们的含义映射到Labeled的 JavaDoc API 。
A detailed description of the attributes is in the css reference guide.
属性的详细说明在css 参考指南中。
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.
这是一个带有样式设置嵌入在 fxml 中的图形的示例,尽管在 tarrsalah 的示例中将样式信息分离到单独的 css 样式表中总是更好。
<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 Potion">
<font>
<Font size="20.0" />
</font>
</Button>
Related solutions for adding images to buttons using only Java code
仅使用Java代码向按钮添加图片的相关解决方案
回答by tarrsalah
FXMLis used just for designing the layout, for styling you can use cssand reference it from your FXML
file :
FXML仅用于设计布局,对于样式,您可以使用css并从您的FXML
文件中引用它:
<stylesheets>
<URL value="@main.css" />
</stylesheets>
To add an image to a fx:id="btn"
button in your css
:
要将图像添加到fx:id="btn"
您的按钮中css
:
#btn {
-fx-background-image: url("Add.png");
-fx-background-size: 18 18;
-fx-background-repeat: no-repeat;
-fx-background-position:left;
}
This Github repositoryprovide a complete running example:
这个Github 存储库提供了一个完整的运行示例:
回答by Vision9000
A couple of changes I made to the css example (ref. #btn { in comment #3):
我对 css 示例进行了一些更改(参考 #btn { 在评论 #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.
这删除了按钮轮廓,只留下图像。我能够像 ImageView 一样在 Scene Builder 中调整按钮选择器的大小,并实时查看图片的变化。我添加了一个工具提示来解释图像,因为没有文本和按钮轮廓。我计划使用其他地方解释的技术并在单击时更改图像以提供图像是按钮的更多视觉反馈。
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.
另请注意:我需要在“CSS Id”(场景生成器)而不是“fx:id”中指定按钮 id 才能将它们链接起来。
Adding these changes to what was explained above allowed me to accomplish my goal: have a button that looks only like the image (e.g. round shape in squarish button).
将这些更改添加到上面解释的内容中使我能够实现我的目标:拥有一个看起来只像图像的按钮(例如方形按钮中的圆形)。