0
我想要的是我的GUI組件(導出爲jar文件,並被其他組件使用)可以通過使用存儲在jar文件旁邊的圖像文件夾中的圖像動態更改圖像。因此,在標籤中使用url不是一種選擇,因爲無論我如何嘗試,我的jxml文件在未包含在jar文件中時都找不到圖像資源。
因此我嘗試這樣的:在javafx中定義<Image/>標記
在我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拋出異常,並要求圖片標記中的網址不應該爲空。
有人可以告訴我我做錯了什麼?
非常感謝。
P/S代碼被簡化爲您的閱讀方便。我正在使用Java 8.
什麼是JXML文件? – 2015-02-23 19:59:38