简体   繁体   English

使用Eclipse导入资源时遇到问题

[英]Trouble Importing Resources with Eclipse

I'm setting a JLabel to show an icon. 我正在设置一个JLabel以显示一个图标。

        public Icon getIcon(String type) throws IOException
    {

    String avatar = "Academic.jpg";
    switch(type)
    {
    case"Academic": avatar ="Academic.jpg"; break;
    case"Alchemist": avatar ="Alchemist.jpg"; break;
    case"Assassin": avatar ="Assassin.jpg"; break;
}
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream input = classLoader.getResourceAsStream(avatar);
    Image logo = ImageIO.read(input);
    Icon icon = new ImageIcon(logo);
    return icon;
}

"input" is still null on the 3rd line, when "avatar" contains a string "Pic.jpg". 当“头像”包含字符串“ Pic.jpg”时,“输入”在第三行仍为空。 My resources folder is in the src folder? 我的资源文件夹位于src文件夹中吗? Have I done something wrong? 我做错什么了吗? How does eclipse know how to find the images? 蚀如何知道如何找到图像?

You are just passing the file name with no path of any kind so unless that file is located at the root of your project, and or classpath it will not be found. 您只是传递没有任何路径的文件名,因此,除非该文件位于项目的根目录下,否则将找不到该文件的类路径。 You will need to add the path details of where the file is located. 您将需要添加文件所在位置的路径详细信息。 Which sounds like it will at least start with: 听起来至少是这样:

/src/resources//Academic.jpg /src/resources//Academic.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM