Java IDE - Eclipse,导入资源 [英] Java IDE - Eclipse, Importing resources

查看:128
本文介绍了Java IDE - Eclipse,导入资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人可以帮助我一些很简单的东西。我是Java编程的新手(2周),在Linux下使用Eclipse IDE。我目前正在创建一个JFrame应用程序,到目前为止,一切都很顺利。



我有一个小小的钩子 - 我已经包括一套图标和将它们分配给JLabel,并显示它们。导出可执行JAR时,它们不在JAR中作为资源。如果我打开JAR文件,我可以看到JAR的ROOT中的图像没有组织在它们各自的文件夹中。 (图标/等)。



TL; DR - 如何将其文件夹中的资源作为资源导入JAR。

  public void drawCategoryIcons(){
for(int i = 0; i< aspCategories.length; i ++){
图标pcIcon = new ImageIcon(getClass .getResource(/ icons /+ cats [i]));

aspCategories [i] = new JLabel(,JLabel.CENTER);
aspCategories [i] .setIcon(pcIcon);
panel.add(aspCategories [i],w 200,center);
}
}

如果我在Eclipse中运行项目,这应该。

解决方案

您可能在eclipse项目中创建了图标目录作为单独的源文件夹(它具有由文件夹图标和包图标组成的图标)。源文件夹只是开发过程中的逻辑元素(因此您可以逻辑地分离应用程序的模块),它们不是导出的应用程序的一部分。



因此,简单的解决方法可能是在其他源文件夹中创建一个图标通常称为src)。导出后,这些包将作为子文件夹存在。


Hopefully someone can help me with something that is probably very simple. I'm new to Java coding (2 weeks in), and using Eclipse IDE under Linux. I'm currently in the process of creating a JFrame application, everything so far is going well.

I have one little snag though - I have included a set of Icons and assigned them to a JLabel, and have them displayed. Upon exporting an Executable JAR, they are not in the JAR as a resource. If I open the JAR file, I can see the Images, in the ROOT of the JAR, not organized in their respective folders. (icons/, etc).

TL;DR - How do I import resources, in their folders, into a JAR as a resource.

    public void drawCategoryIcons() {
    for (int i = 0; i < aspCategories.length; i++) {
        Icon pcIcon = new ImageIcon(getClass().getResource( "/icons/" + cats[i]));

        aspCategories[i] = new JLabel("", JLabel.CENTER);
        aspCategories[i].setIcon(pcIcon);
        panel.add(aspCategories[i], "w 200, center");
    }
}

If I RUN the project within Eclipse, everything works as it should. Exporting it, I get tons of errors.

解决方案

You have probably created the icons directory as separate source folder in the eclipse project (it has an icon composed of a folder icon and a package icon then). Source folders are just logical elements during development (so you can separate modules of your application logically), they are not part of the exported application. Only everything inside source folders is exported.

Therefore a simple workaround might be to instead create a package "icons" in your other source folder (typically called "src"). Those packages will exist as sub folders after export.

这篇关于Java IDE - Eclipse,导入资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆