java.lang.IllegalArgumentException: Invalid URL or resource not found

后端 未结 2 793
灰色年华
灰色年华 2020-12-10 03:53

I tested this code:

public static void main(String[] args)
    {
        Application.launch(args);
    }

    @Override
    public void start(Stage primarySt         


        
2条回答
  •  北海茫月
    2020-12-10 04:53

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

    The constructor of this needs to point to a URI, so if you're pointing to something on the file system it'd be:

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

    Alternatively, you could do:

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

    ...which is arguably neater. If the image is bundled in your jar rather than on the filesystem, you can obtain the URI like so:

    Image image = new Image(getClass().getResource("za.jpg").toURI().toString());
    

    Most methods / constructors in JavaFX that take a string as a parameter in this way (i.e. specifying a resource) do so via string URI's rather than just a plain file path or URL.

提交回复
热议问题