Short answer: Use "/img/smile.png"
.
What's actually happening is that any path starting with /
which is given to the Class.getResource method is always treated as being relative to each entry in the classpath.
As your screenshot shows, the res
directory is such a classpath entry. So the Class.getResource method treats the path you provide as relative to that entry. Meaning, relative to the res
directory.
So, the method combines your string argument with that directory, which results in res/res/img/smile.png
. Since no file (resource) exists at that location, it returns null.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…