Java swing 应用程序找不到图像

发布于 2024-08-29 17:47:59 字数 646 浏览 2 评论 0原文

我正在使用 swing gui 用 java 为学校制作一个鱼雷游戏,请参阅压缩源 这里

我使用存储在 /bin/resource/graphics/default 文件夹的子文件夹中的自定义按钮图标和鼠标光标,其中根文件夹是程序的根文件夹(我想它也是最终 .jar 中的根文件夹)除了“bin”之外,还包含一个包含所有类的“main”文件夹。资源的相对路径存储在MapStruct.java的shipPath和mapPath变量中。 现在,Battlefield.java 的 PutPanel 类找到了它们并正确设置了其按钮的图标,但其他所有类都无法获取其图标,例如 Table.java 的 setCursor,它应该为所选船舶图像的所有元素设置鼠标光标或者 Field.java 的 this.button.setIcon(icon);在构造函数中,应设置“水”按钮的图标。

我通过调试观察了发生的情况,加载后图像保持为空,尽管路径似乎是正确的。我还尝试在图像文件夹中编写测试文件,但该方法返回 filenotfound 异常。我试图获取该类的路径,看看它是否从假定的位置运行,看起来确实如此,所以我现在真的找不到问题。

有人可以帮我吗? 谢谢。

I'm making a torpedo game for school in java with swing gui, please see the zipped source HERE.

I use custom button icons and mouse cursors of images stored in the /bin/resource/graphics/default folder's subfolders, where the root folder is the program's root folder (it will be the root in the final .jar as well I suppose) which apart from "bin" contains a "main" folder with all the classes. The relative path of the resources is stored in MapStruct.java's shipPath and mapPath variables.
Now Battlefield.java's PutPanel class finds them all right and sets up its buttons' icons fine, but every other class fail to get their icons, e.g. Table.java's setCursor, which should set the mouse cursor for all its elements for the selected ship's image or Field.java's this.button.setIcon(icon); in the constructor, which should set the icon for the buttons of the "water".

I watched with debug what happens, and the images stay null after loading, though the paths seem to be correct. I've also tried to write a test file in the image folder but the method returns a filenotfound exception. I've tried to get the path of the class to see if it runs from the supposed place and it seems it does, so I really can't find the problem now.

Could anyone please help me?
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彻夜缠绵 2024-09-05 17:47:59

您需要像这样加载图标:

ClassLoader cl= this.getClass().getClassLoader();
URL imageURL   = cl.getResource("resources/...");
ImageIcon icon = new ImageIcon(imageURL);

并且您需要将资源文件夹添加到 Eclipse 中的类路径中。请注意,该路径不是文件路径,这样,如果您决定将应用程序捆绑在 jar 文件中,它就可以工作。

You need to load icons like this:

ClassLoader cl= this.getClass().getClassLoader();
URL imageURL   = cl.getResource("resources/...");
ImageIcon icon = new ImageIcon(imageURL);

And you need to add your resource folder to the classpath in Eclipse. Note that the path is not a file path, this way it will work if you decide to bundle your app in a jar file.

难得心□动 2024-09-05 17:47:59
btnRegistration.setIcon(createImageIcon("reg.png"));

protected ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = Master.class.getClassLoader().getResource(path);
    if (imgURL != null) {
    return new ImageIcon(imgURL);
    } else {
    System.out.println("Couldn't find file: " + path);
    return null;
    }
}

这里的btnRegistration是我的JButton
师父是我的班级
reg.png 是我的图像,属于我的项目

btnRegistration.setIcon(createImageIcon("reg.png"));

protected ImageIcon createImageIcon(String path) {
    java.net.URL imgURL = Master.class.getClassLoader().getResource(path);
    if (imgURL != null) {
    return new ImageIcon(imgURL);
    } else {
    System.out.println("Couldn't find file: " + path);
    return null;
    }
}

here btnRegistration is my JButton
Master is my Class
and reg.png is my image that is belong in my project

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文