当前位置: 首页 > 知识库问答 >
问题:

IllegalArgumentException:找不到无效的URL或资源

秦博延
2023-03-14
public static void main(String[] args)
    {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage)
    {

        // Image
        Image image = new Image("za.png");
        ImageView imageView = new ImageView();
        imageView.setImage(image);

        // Text
        Text t = new Text();
        t.setText("Do you want to quit?");

        // Buttons
        Button btnYes = new Button("Yes");
        Button btnNo = new Button("No");
        btnYes.setStyle("-fx-background-color:\n"
                + "        #090a0c,\n"
                + "        linear-gradient(#38424b 0%, #1f2429 20%, #191d22 100%),\n"
                + "        linear-gradient(#20262b, #191d22),\n"
                + "        radial-gradient(center 50% 0%, radius 100%, rgba(114,131,148,0.9), rgba(255,255,255,0));\n"
                + "    -fx-background-radius: 5,4,3,5;\n"
                + "    -fx-background-insets: 0,1,2,0;\n"
                + "    -fx-text-fill: white;\n"
                + "    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );\n"
                + "    -fx-font-family: \"Arial\";\n"
                + "    -fx-text-fill: linear-gradient(white, #d0d0d0);\n"
                + "    -fx-font-size: 12px;\n"
                + "    -fx-padding: 10 20 10 20;");

        btnNo.setStyle("-fx-background-color:\n"
                + "        #090a0c,\n"
                + "        linear-gradient(#38424b 0%, #1f2429 20%, #191d22 100%),\n"
                + "        linear-gradient(#20262b, #191d22),\n"
                + "        radial-gradient(center 50% 0%, radius 100%, rgba(114,131,148,0.9), rgba(255,255,255,0));\n"
                + "    -fx-background-radius: 5,4,3,5;\n"
                + "    -fx-background-insets: 0,1,2,0;\n"
                + "    -fx-text-fill: white;\n"
                + "    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );\n"
                + "    -fx-font-family: \"Arial\";\n"
                + "    -fx-text-fill: linear-gradient(white, #d0d0d0);\n"
                + "    -fx-font-size: 12px;\n"
                + "    -fx-padding: 10 20 10 20;");

        // Buttons layout
        HBox hbox = new HBox(8); // spacing = 8
        hbox.setStyle("-fx-padding: 15; -fx-font-size: 15pt;");
        hbox.getChildren().addAll(btnYes, btnNo);
        hbox.setAlignment(Pos.BASELINE_RIGHT);

        BorderPane bp = new BorderPane();
        bp.setStyle("-fx-background-color: linear-gradient(#ffffff,#f3f3f4);\n"
                + "    -fx-border-width: 1 1 1 1;\n"
                + "    -fx-border-color: #b4b4b4 transparent #b4b4b4 transparent;\n"
                + "    -fx-font-size: 1.083333em;\n"
                + "    -fx-text-fill: #292929;");

        bp.setPadding(new Insets(10, 20, 10, 20));
        //Button btnTop = new Button("Top");
        bp.setTop(null);
        //Button btnLeft = new Button("Left");
        bp.setLeft(imageView);
        //Button btnCenter = new Button("Center");
        bp.setCenter(t);
        //Button btnRight = new Button("Right");
        bp.setRight(null);
        //Button btnBottom = new Button("Bottom");
        bp.setBottom(hbox);
        Scene scene = new Scene(bp, 500, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
Executing com.javafx.main.Main from /home/rcbandit/Desktop/test/DX-57DC/dist/run429319394/DX-57DC.jar using platform /opt/jdk1.8.0/bin/java
Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:491)
    at com.javafx.main.Main.launchApp(Main.java:642)
    at com.javafx.main.Main.main(Main.java:805)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:986)
    at javafx.scene.image.Image.<init>(Image.java:538)
    at com.dx57dc.main.DX57DC.start(DX57DC.java:28)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:215)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
    at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:82)
    ... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:979)
    ... 12 more
Java Result: 1
Deleting directory /home/rcbandit/Desktop/test/DX-57DC/dist/run429319394
jfxsa-run:
BUILD SUCCESSFUL (total time: 6 seconds)

共有1个答案

姜俊友
2023-03-14
Image image = new Image("za.png");

它的构造函数需要指向一个URI,所以如果您指向文件系统上的某个东西,它应该是:

Image image = new Image("file:za.png");

或者,您可以这样做:

Image image = new Image(new File("za.png").toURI().toString());

...可以说更整洁。如果图像绑定在jar中而不是文件系统中,则可以像下面这样获得URI:

Image image = new Image(getClass().getResource("za.jpg").toURI().toString());
 类似资料:
  • 我使用Dropbox API V2将图像从Dropbox下载到Java程序中。我将它们保存在中,然后希望在下载后程序运行的同时获得。 1)当程序第一次下载图像并运行时,我会得到“无效URL或资源未找到”错误: 2)第二次,当我关闭程序,文件已经保存在资源中,它工作。 我错过了什么?

  • 正如上面所说的那样。oracle关于图像类: 我在github上分享了我的简单项目,所以你可以很容易地访问代码。 这是我的一段代码: 但这不想为我工作,我明白了: 原因:java。lang.IllegalArgumentException:在javafx上找不到无效的URL或资源。场景形象形象validateUrl(Image.java:1081) 在(Image.java:1081)我发现: 我

  • 我使用的是NetBeans IDE 8.2。这是我正在使用的代码: 加载到cats阵列中的图像位于名为“img”的src文件夹中,编号为1-6。当我从NetBeans IDE运行程序时,它运行得非常完美;然而,当我试图在构建项目后创建的dist文件夹中执行JAR文件时,它根本不会运行。当我尝试使用命令行运行它时,我得到以下结果: 我假设问题是JAR文件无法访问图像文件,但我不知道如何修复它。如何将

  • 问题内容: 这是logcat: 与之关联的行是: 可能是什么问题呢?创建数据库就好了。您还需要查看其他代码吗? 更新: 我非常确定此列存在。我用这个查询数据库: 在LogCat中: 因此该列确实存在。 问题答案: 我发现了一个奇怪的解决方案。在中。你所要做的:

  • 我检查以确保SDK找到SDK中包含的证书。所以我猜这是一个无效的证书。我现在不知道该怎么做了。有人知道如何解决这个问题吗?

  • 我正试图用相机拍照,但出现以下错误: AndroidManifest。xml: Java: 文件路径。xml 我整天都在搜索这个错误,试图理解,但我不知道这个错误消息试图告诉我什么。如果您需要更多信息/代码,请在评论中给我写信。