简体   繁体   English

在 Java 中使用 ImageIcon 时无法在 Java 中选择默认路径

[英]Cant choose a default path in java when using ImageIcon in Java

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每当我需要加载一些图像时,我的路径都会遇到困难,就像我使用 eclipse 并将其放在 src 文件夹中,然后使用此路径一样

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谢谢

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM