我只想加载一张图片。
在较旧的程序中,它可以正常工作。唯一的区别是,旧的使用目录,而这个使用包。实际上我甚至无法创建目录。
这就是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;
}
}
答案 0 :(得分:0)
问题是,图像基本上在src目录中。