简体   繁体   English

在使用文件系统时需要帮助

[英]Need help using the file system

I'm trying to load an image onto a JPanel. 我正在尝试将图像加载到JPanel上。 I define an Image as this: 我将图像定义为:

    Image image = ImageIO.read(new File("~/Desktop/Example.png"));

This line returns the following error: 此行返回以下错误:

Exception in thread "main" javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at Background.<init>(Background.java:16)

(This is at line 16 of my code) (这是我代码的第16行)

The tilde character ~ is expanded by a shell so needs to be replaced to be used in Java. 波浪号~由外壳扩展,因此需要替换才能在Java中使用。 You could do 你可以做

Image image = 
    ImageIO.read(new File(System.getProperty("user.home") + "/Desktop/Example.png"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM