Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我尝试制作 ImageIcon i = new ImageIcon("source"); 代码不会像我想要的那样从 eclipse 包中读取它,但是当我告诉它从 C:/... 文件中读取它时,它可以工作。为什么会这样,我该如何解决?
new ImageIcon("source")期待File参考。在应用程序中维护的资源(通常)不被视为文件,相反,它们被视为资源。
new ImageIcon("source")
File
这意味着您无法使用您可能习惯的“noraml”方法访问它们。
相反,请尝试使用getClass().getResource("/path/to/resource/source")
getClass().getResource("/path/to/resource/source")
路径是源树顶部的路径...