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

后端 未结 2 792
灰色年华
灰色年华 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:38

    Do

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

    Or simply

    ImageView iv = new ImageView(getClass().getResource("za.jpg").toExternalForm());
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题