简体   繁体   English

JavaFX如何从SceneBuilder设置ImageView

[英]JavaFX How set ImageView from SceneBuilder

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). 正如我在SceneBuilder看到的SceneBuilder您可以为ImageView设置图形(尽管如果将项目导出为.jar并将图像放入jar中,该图像将无法工作)。

1) I want to set images for some fxml buttons through SceneBuilder but i can't find anything about that. 1) 我想通过SceneBuilder为一些fxml按钮设置图像,但是我找不到任何有关此的信息。

2) Also it will be usefull if you provide info on how to do with .fxml what i do with getClass().getResource("image.png"); 2) 此外 ,如果你提供有关如何做的信息将是有用的.fxml我做什么用getClass().getResource("image.png"); ,so i have not to do this on the java file manually. ,所以我不必手动在Java文件上执行此操作。

Edit: 编辑:

Both answers from Fabian and are John Vernee are correct each one for question 1 and 2 but i can choose only one. 来自FabianJohn Vernee的两个答案对于问题1和2都是正确的,但我只能选择一个。

The structure of resources folder: 资源文件夹的结构:

在此处输入图片说明

In SceneBuilder drag the graphic node you want to use (in this case ImageView ) from the Library to the Button in the Hierarchy view. 在SceneBuilder中,将要使用的graphic节点(在本例中为ImageView )从“ 库”拖到“ 层次结构”视图中的“ Button ”。 Drop it there and you should see the graphic node displayed as a child of the Button . 将其放在此处,您将看到graphic节点显示为Button的子级。

This will result in a fxml like this 这将导致这样的fxml

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

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 . 通过使用ImageView的image属性旁边的齿轮按钮,可以通过选择“ 切换到文档相对路径”来指定以@开头的图像URL。 Input image.png for the url @image.png . 输入image.png作为网址@image.png Saving the fxml to the correct location relative to the image will display the image in SceneBuilder. 将fxml保存到相对于图像的正确位置将在SceneBuilder中显示图像。

You can set the url for an ImageView in FXML like this: 您可以像这样在FXML中设置ImageView的网址:

<ImageView>
    <image>
        <Image url="@my_image.png" />
    </image>
</ImageView>

It will look in the same folder as the FXML file for the image. 它将与图像的FXML文件位于同一文件夹中。


You could set the graphic of a Button by referencing it from FXML: 您可以通过从FXML引用Button来设置Button的图形:

<!-- Define some ImageView -->
<fx:define>
    <ImageView fx:id="my_image">
        <image>
            <Image url="@my_image.png" />
        </image>
    </ImageView>
</fx:define>

<!-- Reference it here -->
<Button graphic="$my_image"/>

I'm building my jar using apache maven 's plugin for eclipse, using the javafx-jar-plugin . 我正在使用javafx-jar-plugin使用apache maven的eclipse插件构建javafx-jar-plugin

If you've worked with maven before, you can use this plugin in pom.xml : 如果您以前使用过maven,则可以在pom.xml使用此插件:

<build>
    <plugins>
        <plugin>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.5.0</version>
            <configuration>
                <mainClass>application.MainFXML</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

I've put the FXML file and the image in the same folder under src/main/resources , so they get included into the jar. 我将FXML文件和图像放在src/main/resources下的同一文件夹中,因此它们被包含在jar中。

Run maven build with the jfx:jar goal. 使用jfx:jar目标运行maven build。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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