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

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

每当我需要加载一些图像时 我对它的路径有困难 就像我使用 eclipse 并将其放在 src 文件夹中然后使用此路径

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

它不加载图像我需要把它放在我电脑的某个地方然后复制粘贴它的路径....它实际上不是更好的选择因为当我倾向于 运行 我的代码在其他电脑上我需要再次更改代码中所有图像的路径 请帮忙

谢谢

在您的项目文件夹中创建 "resource" 文件夹,然后将图像粘贴到该文件夹​​中,然后尝试按照

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

或者像下面这样使用 bufferedImage

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