图片 url 是如何用 java fxml 写的
Posted
技术标签:
【中文标题】图片 url 是如何用 java fxml 写的【英文标题】:How the image url is wrriten in java fxml 【发布时间】:2019-07-02 20:49:10 【问题描述】:我正在使用 fxml 文件,但我不知道如何将 url(路径)写入我拥有的图像,我尝试了绝对路径和目录路径
我收到一个无效的 url 错误,例如 >>
Feb 08, 2019 5:09:27 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 11.0.1 by
JavaFX runtime of version 10.0.2
null/bars-chart.png
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: javafx.fxml.LoadException:
unknown path:64
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2450)
at back_end.image_main.start(image_main.java:25)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1107)
at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:688)
at javafx.fxml/com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:47)
at javafx.fxml/com.sun.javafx.fxml.builder.JavaFXImageBuilder.build(JavaFXImageBuilder.java:37)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:771)
at javafx.fxml/javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2838)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2557)
... 11 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1099)
... 17 more
Exception running application back_end.image_main
我的项目结构是
fxml_image :
src :
front_end :
>(bars-chart.png)
>(main.fxml)
back_end :
>(image_main.java)
这个块是我的(main.fxml)的一部分>>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style=" " text="Drag components from Library here…" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
<font>
<Font size="18.0" />
</font>
</Label>
<ImageView fitHeight="349.0" fitWidth="606.0" layoutX="12.0" layoutY="11.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@bars-chart.png" />
</image>
</ImageView>
</children>
</AnchorPane>
这是java文件(image_main)
package back_end;
import java.io.FileInputStream;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class image_main extends Application
public static void main(String[] args) Application.launch(args);
@Override
public void start(Stage stage) throws IOException
// Create the FXMLLoader
FXMLLoader loader = new FXMLLoader();
// Path to the FXML File
String fxmlDocPath = "src/front_end/main.fxml";
FileInputStream fxmlStream = new FileInputStream(fxmlDocPath);
// Create the Pane and all Details
VBox root = (VBox) loader.load(fxmlStream);
// Create the Scene
Scene scene = new Scene(root);
// Set the Scene to the Stage
stage.setScene(scene);
// Set the Title to the Stage
stage.setTitle("A FXML Example which includes FXML Files");
// Display the Stage
stage.show();
是否有另一种加载 FXML 的方法,如何解释图像链接
【问题讨论】:
请遵守 java 命名约定 【参考方案1】:我不建议您将图像文件包含在“src”文件夹中。您应该创建一个名为“res”的新文件夹并将其作为源文件夹(选项 -> 构建路径 -> 用作源文件夹)。将您的图像放在“res”文件夹中。然后在fxml文件中做如下修改。
<Image url="@../bars-chart.png" />
我希望这对你有用。
【讨论】:
【参考方案2】:使用 fxml 文件的位置解析路径。如果您使用 InputStream
作为 fxml 数据的源来加载 fxml,FXMLLoader
无法确定位置 url。
改为指定位置:
FXMLLoader loader = new FXMLLoader(new File("src/front_end/main.fxml").toURI().toURL());
VBox root = (VBox) loader.load();
假设 fxml 是一种资源,我建议使用 getResource
来获取 url:
FXMLLoader loader = new FXMLLoader(getClass().getResource("/front_end/main.fxml"));
【讨论】:
以上是关于图片 url 是如何用 java fxml 写的的主要内容,如果未能解决你的问题,请参考以下文章
Java开发桌面程序学习——ImageView设置图片以及jar包读取fxml文件
javaFX如何用Button实现跳转到一个已创建的fxml界面