【问题标题】:JavaFx - java.lang.IllegalArgumentException: Invalid URL or resource not foundJavaFx - java.lang.IllegalArgumentException:无效的 URL 或找不到资源
【发布时间】:2019-02-02 04:14:32
【问题描述】:

我最近开始学习 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

【问题讨论】:

标签: javafx illegalargumentexception


【解决方案1】:

您可以在项目中设置资源文件夹,以防您想将其包装在 jar 文件中并像这样加载它们
new Image(getClass.getResourceAsStream("image/yourImage.gif"))
要么
如果这些图片不在您的资源中,您可以像这样加载它们
new Image("file:image/yourimage.gif")

【讨论】:

  • 我不建议这样做。这样,您就可以将工作目录作为包含image 目录的目录。您不能保证这一点,通常最好将资源包含在 classpath/jar 中,这是 Image 查找图像的位置,如果 URL 是相对的。
  • 他没有说这些图片是否是资源图片 r 如果是图库应用,所以这些图片不在应用资源中
  • 如果你想使用文件作为源,可能会更好use the File class to get the URL
  • 我觉得和第二种方案一样
猜你喜欢
  • 1970-01-01
  • 2013-06-04
  • 2015-04-13
  • 1970-01-01
  • 2014-10-25
  • 2016-01-30
  • 1970-01-01
  • 2019-12-02
  • 2015-06-14
相关资源
最近更新 更多