【问题标题】:IntelliJ javax.imageio.IIOException: Can't read input fileIntelliJ javax.imageio.IIOException:无法读取输入文件
【发布时间】:2017-08-03 08:07:22
【问题描述】:

我只是想加载一张图片。

在较旧的程序中它可以工作。唯一的区别是,旧的使用目录,而这个使用包。实际上我什至不能创建目录。

这就是 Stacktrace:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at com.company.ImageLoader.loadImage(ImageLoader.java:17)
    at com.company.Window.Window.<init>(Window.java:30)
    at com.company.Engine.<init>(Engine.java:21)
    at com.company.Main.main(Main.java:8)

public class Window
{
    private JFrame myFrame;
    private JPanel mainPanel;
    private JLabel mainLabel;
    private ImageLoader myImageLoader = new ImageLoader();

    public Window(Boolean defaultLaFDeco, String title, int x, int y, int width, int height)
    {
        JFrame.setDefaultLookAndFeelDecorated(defaultLaFDeco);
        myFrame = new JFrame();
        myFrame.setTitle(title);
        mainPanel = new JPanel();
        mainLabel = new JLabel();
        mainPanel.add(mainLabel);
        myFrame.add(mainPanel);
        myFrame.setBounds(x,y,width,height);
        myFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        myFrame.setVisible(true);

        File file = new File("Res/cat/0.jpg");
        BufferedImage img = myImageLoader.loadImage(file);
    }
}

public class ImageLoader
{
    private BufferedImage image = null;

    public BufferedImage loadImage(File file)
    {
        BufferedImage img = null;
        try
        {
            img = ImageIO.read(file);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        return img;
    }
}

【问题讨论】:

  • 您不能在包树中创建目录。试试new File("../Res/cat/0.jpg");

标签: java image intellij-idea io


【解决方案1】:

问题是,图像基本上在 src 目录中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 2015-09-02
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多