javafx - JavaFx - java.lang.IllegalArgumentException:无效的 URL 或找不到资源
问题描述
我最近开始学习 JavaFX,我的班级被分配了以下作业:
编写一个程序,在网格窗格中显示四个图像。
这是我的代码:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class Ch_14_Ex_1 extends Application {
@Override //Override the start method in the Application class
public void start(Stage primaryStage) {
//Create a pane to hold the image views
GridPane pane = new GridPane();
//Place nodes in the pane
pane.add(new ImageView(new Image("image/ukFlag1.gif")), 0, 0);
pane.add(new ImageView(new Image("image/canadaFlag1.gif")), 1, 0);
pane.add(new ImageView(new Image("image/chineseFlag1.gif")), 0, 1);
pane.add(new ImageView(new Image("image/usaFlag1.gif")), 1, 1);
//Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("Ch_14_Ex_1"); //Set the stage title
primaryStage.setScene(scene); //Place the scene in the stage
primaryStage.show(); //Display the stage
}
}
该程序编译得很好,但是每当我尝试运行该程序时,我都会收到以下错误,我不确定如何修复它。
Exception in Application start method
Exception in thread "JavaFX BlueJ Helper" java.lang.RuntimeException:
Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1118)
at javafx.scene.image.Image.<init>(Image.java:620)
at Ch_14_Ex_1.start(Ch_14_Ex_1.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1110)
... 11 more
解决方案
您可以在项目中设置资源文件夹,以防您想将其包装在 jar 文件中并像这样加载它们,
new Image(getClass.getResourceAsStream("image/yourImage.gif"))
或者
如果这些图像不在您的资源中,您可以像这样加载它们
new Image("file:image/yourimage.gif")
推荐阅读
- python - Django如何通过它的ID(不是输入名称)获取输入值
- php - 从 s3bucket 下载一个 zip 文件,解压缩并将所需文件保存到本地文件路径中.. 使用 php
- sql - 计算 DateDiff 的查询返回条件中的数据类型不匹配
- javascript - javascript中数组和对象的比较
- botframework - 我缺少什么配置文件,如何找到或生成它?
- airflow - 使用 Dataflowpythonoperator 编排 Dataflow 作业
- soap - Onvif 相对移动请求失败
- date - 如何获取dll文件的编译日期?
- arraylist - 如何使用java8将对象列表取消组合为另一个对象的列表
- boolean - 当布尔运算符总是排除单个值,而根据逻辑应该包含它时,如何解决它?