Cant choose a default path in java when using ImageIcon in Java

南笙酒味 提交于 2019-12-25 18:18:08

问题


Whenever i need to load some image i have difficulties with its path Like i use eclipse and by putting it in src folder and then using this path

ImageIcon = new ImageIcon ("/image.png");

It does not load image i need to put it somewhere in my computer and then copy paste its path ....Its not Actually better option as when i tend to run my code in some other computer i need to change the paths of all images in my code back again please help

Thanks


回答1:


Create "resource" folder into your project folder than paste your image into that folder and try following

imageIcon ii = new imageIcon(getClass().getResource("/image.png"));

Or use bufferedImage like following

BufferedImage bi = ImageIO.read(getClass().getResource("/image.png");


来源:https://stackoverflow.com/questions/46745962/cant-choose-a-default-path-in-java-when-using-imageicon-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!