我有一个带图像文件夹的eclipse项目。我需要从位于此文件夹中的图像创建一个ImageIcon,但无法弄清楚如何告诉它在文件夹中查找,而无需在我的系统上给出确切的位置,我不想在项目移动到不同的位置PC的。带ImageIcon的Eclipse项目
代码示例
ImageIcon ai = new ImageIcon("/somedir");
感谢您的帮助。
我有一个带图像文件夹的eclipse项目。我需要从位于此文件夹中的图像创建一个ImageIcon,但无法弄清楚如何告诉它在文件夹中查找,而无需在我的系统上给出确切的位置,我不想在项目移动到不同的位置PC的。带ImageIcon的Eclipse项目
代码示例
ImageIcon ai = new ImageIcon("/somedir");
感谢您的帮助。
您必须将图像文件夹保存到您的projectand则:
java.net.URL imageURL = this.getClass().getClassLoader().getResource("path_to_image_folder");
ImageIcon aceOfDiamonds = new ImageIcon(imageURL);
到图像文件夹的路径可以是相对于你的类或绝对的(以“/”开头)。
你可以在这里看看:How to use icons
这很容易(删除反斜线):
ImageIcon ai = new ImageIcon("somedir");
你能告诉我们你想刚才的代码? – 2010-10-12 10:43:08