java.lang.IllegalArgumentException:无效的 URL 或找不到资源 [英] java.lang.IllegalArgumentException: Invalid URL or resource not found

查看:90
本文介绍了java.lang.IllegalArgumentException:无效的 URL 或找不到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试了这段代码:

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

        btnNo.setStyle("-fx-background-color:
"
                + "        #090a0c,
"
                + "        linear-gradient(#38424b 0%, #1f2429 20%, #191d22 100%),
"
                + "        linear-gradient(#20262b, #191d22),
"
                + "        radial-gradient(center 50% 0%, radius 100%, rgba(114,131,148,0.9), rgba(255,255,255,0));
"
                + "    -fx-background-radius: 5,4,3,5;
"
                + "    -fx-background-insets: 0,1,2,0;
"
                + "    -fx-text-fill: white;
"
                + "    -fx-effect: dropshadow( three-pass-box , rgba(0,0,0,0.6) , 5, 0.0 , 0 , 1 );
"
                + "    -fx-font-family: "Arial";
"
                + "    -fx-text-fill: linear-gradient(white, #d0d0d0);
"
                + "    -fx-font-size: 12px;
"
                + "    -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);
"
                + "    -fx-border-width: 1 1 1 1;
"
                + "    -fx-border-color: #b4b4b4 transparent #b4b4b4 transparent;
"
                + "    -fx-font-size: 1.083333em;
"
                + "    -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)

我将图像文件放在 java 源代码文件旁边,但找不到该文件.你能告诉我如何解决这个问题吗?

I placed the image file next to the java source code file but the file is not found. Can you tell me how to fix this?

推荐答案

javafx.scene.image.Image image = new javafx.scene.image.Image(getClass().getResource("za.jpg").toExternalForm());
ImageView iv = new ImageView(image);

或者干脆

ImageView iv = new ImageView(getClass().getResource("za.jpg").toExternalForm());

这篇关于java.lang.IllegalArgumentException:无效的 URL 或找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆