导出 Swing 应用程序后没有图标

发布于 2024-12-29 14:17:25 字数 261 浏览 5 评论 0原文

我正在用 Java 编写 GUI(当然使用 Swing),在 Eclipse 中运行我的应用程序时,一切都很好,并且带有图标等,但是在导出我的应用程序后,图标就消失了。

图标位于以下目录 resources/icons 的类路径下。

JButton browseButton = new JButton("Browse", new ImageIcon("resources/icons/browse.png"));

I'm programming a GUI in Java (using Swing of course) and while running my application in Eclipse everything is fine and with icons and whatnot, but after exporting my application the icons are gone.

The icons are located under the class-path in the following directory resources/icons.

JButton browseButton = new JButton("Browse", new ImageIcon("resources/icons/browse.png"));

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

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

发布评论

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

评论(1

半城柳色半声笛 2025-01-05 14:17:25

似乎按照@kleopatra 的指导,这肯定是正确的方法。
创建资源文件夹时,请确保通过右键单击项目来完成此操作,然后转到源文件夹并将其命名为资源/图标,然后手动将图像放入该文件夹中,然后返回 Eclipse 并刷新项目。现在您也可以在可运行的 jar 中使用这些图像。

现在,为了在您的类中使用您的图像/图标,请使用此

private ImageIcon getImage(String path)
{
    URL url = getClass().getResource(path);
    System.out.println(url);
    if (url != null)
        return (new ImageIcon(url));
    return null;
}

路径,您的path将是image的名称,前缀为正斜杠,位于您的< code>resources/icons 文件夹,如 "/myIcon.png"
在发布答案之前,我想确定一下,所以对其进行了测试。
否则请给我一些时间,我会将所有步骤放在某个地方供您查看。

与整个事情相关的更多信息可以在我的另一个答案中找到:

Load ImageIcon Exception

Seems like as guided by @kleopatra, this surely is the right way.
When you create resources folder, make sure you do it by right clicking your project, then go to source folder and name it as resources/icons and then manually put your images in that folder, and go back to your eclipse and refresh your project. Now you can use these images in your runnable jar also.

Now in order to use your image/icon in your Classes use this

private ImageIcon getImage(String path)
{
    URL url = getClass().getResource(path);
    System.out.println(url);
    if (url != null)
        return (new ImageIcon(url));
    return null;
}

Here your path will be the name of the image prefix it by a forward slash, which is inside your resources/icons folder like "/myIcon.png"
I wanted to be sure, before i post my answer, so tested it.
Else give me some time, I will put all steps somewhere for you to see.

More info related to the whole thingy can be found on this another answer of mine :

Load ImageIcon Exception

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