【发布时间】:2011-12-03 23:37:30
【问题描述】:
我创建了一个applet jar。该 jar 包含以下文件夹中的图像
com\common\images\red.bmp
现在,我想在 Swing Applet 上显示此图像。
private static final ImageIcon redIndicator = new ImageIcon("com\\common\\images\\red.bmp");
之后,我已将 redIndicator 附加到 JPanel,但我无法看到此图像。
有什么建议吗?
===================================已编辑============ ==============================
private static final ImageIcon marker = loadImage("com/common/images/scale.jpg");
@SuppressWarnings("unused")
private static ImageIcon loadImage(String imagePath) {
BufferedInputStream imgStream = new BufferedInputStream(TpcHandler.class.getResourceAsStream(imagePath));
int count = 0;
if (imgStream != null) {
byte buf[] = new byte[2400];
try {
count = imgStream.read(buf);
} catch (java.io.IOException ioe) {
return null;
} finally {
if (imgStream != null)
try {
imgStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (count <= 0) {
LOGGER.warning("Empty image file: " + imagePath);
return null;
}
return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf));
} else {
LOGGER.warning("Couldn't find image file: " + imagePath);
return null;
}
}
我收到以下异常
java.io.IOException: Stream closed
在线count = imgStream.read(buf);
【问题讨论】:
-
Jar 中路径的部分由“/”分隔,而不是“\”(在每个操作系统上)。