2017-08-03 269 views
0

我只是想要加載圖像。IntelliJ javax.imageio.IIOException:無法讀取輸入文件

在一個較舊的程序中,它的工作原理。唯一的區別是,舊的使用目錄,這個使用包。其實我甚至不能創建一個目錄。

那棧跟蹤:

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) 

enter image description here

enter image description here

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; 
    } 
} 
+0

您不能在包樹中創建目錄。嘗試''新的文件(「../ Res/cat/0.jpg」);'' –

回答

0

的問題是,該圖像基本上在src目錄。