在FXML中引用类资源 [英] Referencing class resource in FXML

查看:141
本文介绍了在FXML中引用类资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Maven的JavaFX程序,带有标准的maven文件树:

I have a JavaFX program that uses Maven, with a standard maven file tree:

如何在layout.fxml中引用logo.png,假设布局正在被夸大调用

How do I reference logo.png from within layout.fxml, assuming that layout is being inflated by a call to

Parent root = FXMLLoader.load(getClass().getResource("/fxml/layout.fxml"));

MainApp 中。

我试图使用

<image>
<Image url="@/graphics/logo.png" />
</image>

并在该文件路径上尝试了很多变化(包含和不包含 @ ),但继续抛出

and have tried many variations on that file path (with and without the @), but keep throwing a

java.lang.reflect.InvocationTargetException 
... 
Caused by: java.lang.IllegalArgumentException: URL must not be empty

创建URL的行

推荐答案

您正在尝试找到名为backButton.png的图像资源,而它应该是logo.png。

You are trying to locate the image resource with name "backButton.png" whereas it should be "logo.png".

如果问题仍然存在,请尝试

If the problem persists, try as

<Image url="@../graphics/logo.png" />

来自官方FXML教程


位置解析运算符(由
属性值的@前缀表示)用于指定属性值应为
,将其视为 relative 的位置当前文件而不是
简单字符串。

The location resolution operator (represented by an "@" prefix to the attribute value) is used to specify that an attribute value should be treated as a location relative to the current file rather than a simple string.

这篇关于在FXML中引用类资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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