我制作的applet从URL加载图像。当我使用AppletViewer在Eclipse中运行此applet时,它会显示图像。但是,当我在Tomcat 7中部署applet后在浏览器中运行applet时,图像不显示,只显示applet的灰色背景,并且没有错误打印到控制台。
我也尝试将图像打包在jar中并从那里加载,但是我遇到了同样的问题(在Eclipse中工作但在Tomcat中没有)。
Tomcat是问题还是其他问题?
以下是完整的代码:
import java.awt.Image;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class LoadImage extends JApplet
{
Image image;
public LoadImage()
{
try {
URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/2/2d/Morus_bassanus_adu.jpg/50px-Morus_bassanus_adu.jpg");
image = ImageIO.read(url);
setSize(300, 300);
JLabel label = new JLabel(new ImageIcon(image));
add(label);
setVisible(true);
}
catch (Exception e) {
System.out.println(e.toString());
}
}
}
答案 0 :(得分:1)
尝试
URL url = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/2/2d/Morus_bassanus_adu.jpg/50px-Morus_bassanus_adu.jpg");
BufferedImage image = ImageIO.read(url);
JLabel label = new JLabel(new ImageIcon(image));
label.setMinimumSize(new Dimension(200,200));
this.add(label);