在Java中使用ImageIcon时,不能在java中选择默认路径 [英] Cant choose a default path in java when using ImageIcon in Java
问题描述
每当我需要加载一些图像时,
i的路径有困难
就像我使用eclipse并将其放入src文件夹然后使用此路径
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
谢谢
推荐答案
在项目文件夹中创建resource文件夹,而不是将图像粘贴到该文件夹中并尝试关注
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"));
或者像下面这样使用bufferedImage
Or use bufferedImage like following
BufferedImage bi = ImageIO.read(getClass().getResource("/image.png");
这篇关于在Java中使用ImageIcon时,不能在java中选择默认路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!