【发布时间】:2015-04-25 06:42:03
【问题描述】:
我想要的是我的 GUI 组件(导出为 jar 文件,并由另一个组件使用)可以通过使用存储在其 jar 文件旁边的图像文件夹中的图像来动态更改图像。因此,在标签中使用 url 不是一种选择,因为无论我如何尝试,当我的 jxml 文件不包含在 jar 文件中时,它都无法找到图像资源。
因此我尝试这样:
在我的 avatar.jxml 文件中
<ImageView>
<image>
<Image fx:id="myImage"/>
</image>
</ImageView>
在我的 Java 文件中
public Image myImage = new Image("location of an image stored on computer");
URL location = getClass().getResource("avatar.fxml");
ResourceBundle resources = ResourceBundle.getBundle("myResource");
FXMLLoader fxmlLoader = new FXMLLoader(location, resources);
Pane root = (Pane)fxmlLoader.load();
MyController controller = (MyController)fxmlLoader.getController();
但是当我尝试运行程序时,javaFX 抛出异常并且要求 Image 标签中的 url 不应该为空。
有人可以告诉我我做错了什么吗?
非常感谢。
P/S 为了您的阅读方便,代码已被简化。我正在使用 Java 8。
【问题讨论】:
-
什么是jxml文件?