JavaFX如何从SceneBuilder设置ImageView [英] JavaFX How set ImageView from SceneBuilder

查看:1490
本文介绍了JavaFX如何从SceneBuilder设置ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在 SceneBuilder 中看到的那样,您可以为 ImageView 设置图形(尽管如果导出该图像将不起作用项目为 .jar ,你将图像放入jar中。

As i see in SceneBuilder you can set graphic for ImageView (though that image will not work if you export the project as .jar and you have the image into the jar).

1)我想设置一些fxml按钮的图像通过 SceneBuilder 但我找不到任何相关内容。

1)I want to set images for some fxml buttons through SceneBuilder but i can't find anything about that.

2)另外如果您提供有关如何使用 .fxml 的信息,那将是有用的 getClass()。getResource( image.png); ,所以我不必手动对java文件执行此操作。

2)Also it will be usefull if you provide info on how to do with .fxml what i do with getClass().getResource("image.png");,so i have not to do this on the java file manually.


修改:

Fabian 的答案都是 John Vernee 对于问题1和2的每一个都是正确的,但我只能选择一个。

Both answers from Fabian and are John Vernee are correct each one for question 1 and 2 but i can choose only one.

资源文件夹的结构:

推荐答案

在SceneBuilder中,从 graphic 节点(在本例中为 ImageView ) >库到 Hierarchy 视图中的按钮。把它放在那里你应该看到 graphic 节点显示为 Button 的子节点。

In SceneBuilder drag the graphic node you want to use (in this case ImageView) from the Library to the Button in the Hierarchy view. Drop it there and you should see the graphic node displayed as a child of the Button.

这将导致像这样的fxml

This will result in a fxml like this

...
<Button mnemonicParsing="false" text="Button">
    <graphic>
        <ImageView fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
    </graphic>
</Button>
...

使用<$的image属性旁边的齿轮按钮c $ c> ImageView 您可以通过选择切换到文档相对路径来指定以 @ 开头的图像URL。输入 image.png 获取网址 @ image.png 。将fxml保存到相对于图像的正确位置将在SceneBuilder中显示图像。

By using the gear button next to the image property of the ImageView you can specify a image url starting with @ by selecting Switch to document relative path. Input image.png for the url @image.png. Saving the fxml to the correct location relative to the image will display the image in SceneBuilder.

这篇关于JavaFX如何从SceneBuilder设置ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆