1
package common;

import java.awt.image.BufferedImage;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ResourcesToAccess {
    public static Icon sharedAbstractDownIcon;
    public static Icon sharedAbstractPlayIcon;
    public static Icon sharedAbstractPauseIcon;
    public static Icon sharedAbstractBlackCursor;
    public static Icon sharedAbstractWhiteCursor;
    public ResourcesToAccess(){
        InputStream is = this.getClass().getClassLoader().getResourceAsStream("/src/images/blackCursor.png");
        try{
            BufferedImage bi = ImageIO.read(is);
            sharedAbstractBlackCursor = (Icon) new ImageIcon(bi);
            new JFrame().add(new JLabel(sharedAbstractBlackCursor)).setVisible(true);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new ResourcesToAccess();
    }
}

我正在使用此代码查看是否PNG可以正确加载图像以创建 JLabels、图标等,但我收到以下错误:

java.lang.IllegalArgumentException: input == null!
    at javax.imageio.ImageIO.read(Unknown Source)
    at common.ResourcesToAccess.<init>(ResourcesToAccess.java:21)
    at common.ResourcesToAccess.main(ResourcesToAccess.java:29)  

为什么我会看到该错误消息?

4

1 回答 1

4

尝试

this.getClass().getClassLoader().getResourceAsStream("images/blackCursor.png")

它正在您的类路径中查找,因此src那里没有更多目录

于 2012-12-24T17:52:49.653 回答