我把它包含我的應用程序,在一個名爲com.example.GreatApp.resources
,然後包裝使用加載它的jar:
getClassLoader().getResourceAsStream(
"com/example/GreatApp/resource/icon.png");`
更新。完整的例子
/**
* Creates and returns an ImageIcon from the resource in jar.
* @param location resource location in the jar,
* like 'com/example/GreatApp/res/icon.png' or null if it was not found.
*/
public ImageIcon createImageIconFromResource(String location)
throws java.io.IOException {
java.io.InputStream input = getClassLoader().getResourceAsStream(
location);
// or throw an ioexception here aka `file not found`
if(input == null) return null;
return new ImageIcon(ImageIO.read(input));
}
這是否工作或做你需要做的`新的ImageIcon(的getClass()。的getResource( 「/ COM/...」))`?我不認爲`ImageIcon(String)`構造函數從類路徑加載。 – 2010-12-09 17:04:48
哎呀,你是對的 – javamonkey79 2010-12-09 17:09:46