一个Global类 的部分方法
public class Global{
// 图片目录URL
public static final String RESOURCE_DIR = "resource/";
//配置文件名
public static final String IMAGE_ICON = "icon.gif";
private static Map imagesMap = new HashMap();
public static Image getImage(String name) {
Object o = imagesMap.get(name);
if (o != null) {
return (Image) o;
} else {
String imagePath = RESOURCE_DIR + name;
InputStream is = Global.class.getResourceAsStream(imagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
try {
while ((c = is.read()) >= 0) {
baos.write;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Image image = Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
imagesMap.put(name,image);
return image;
}
}
}
Image image = Toolkit.getDefaultToolkit().getImage(Global.class.getResource(name));
应该也可以,呵