用于ImageIcon的getResources()-Java [英] getResources() for ImageIcon - java

查看:209
本文介绍了用于ImageIcon的getResources()-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个问题:

几天来我一直在努力解决这个问题,但我直言不讳. 以下是一些代码和我的项目结构.

I've been trying to figure this out for a couple of days, but I got to the point I lost my patience. The following are some code and my project structure.

问题::如何使getResources()在eclipse中工作并导入到jar中之后?

QUESTION: how can I get getResources() to work in eclipse and after being imported to jar?

感谢您的帮助.

public enum Icons {
    XXX("src/resoruces/icons/xyz.png");
    private ImageIcon icon;
    Icons(String path) {
       try {
           // will fail miserably in eclipse and after exporting to jar
           URL imageURL = getClass().getClassLoader().getResource(path);
           icon = new ImageIcon(imageURL);
       } catch (Exception e) {
           // works like a char in eclipse and after creating the jar file
           // with the files in the same directory
           System.out.println("getResoruce() did not work");
           icon = new ImageIcon(path);
       }
   }

推荐答案

如果png文件已与原始src目录中的png文件一起打包到JAR中,则

If the png files have been packaged into the JAR in the same locations as they are in the original src directory then

XXX("resources/icons/xyz.png");

应该产生正确的结果.

这篇关于用于ImageIcon的getResources()-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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