Java JEditorPane 不显示图像 [英] Java JEditorPane not displaying images
问题描述
我无法使用 JEditorPane 将 HTML img 标记呈现为图像.显示的只是一个占位符图形.下面是我的代码.提前致谢.
I am unable to get a JEditorPane to render an HTML img tag as an image. All that is displayed is a placeholder graphic. Below is my code. Thanks in advance.
我所看到的:
我的代码:
import java.awt.*;
import java.io.File;
import java.net.URL;
import java.util.Hashtable;
import javax.swing.*;
import javax.swing.text.html.HTMLEditorKit;
public class test
{
private static Hashtable image_cache;
public static void main(String[] args)
{
image_cache = new Hashtable();
URL img_url = null;
try
{
img_url = new File("C:/img/mypic.png").toURI().toURL();
Image img = Toolkit.getDefaultToolkit ().createImage (img_url);
image_cache.put(img_url.toURI(), img);
}
catch (Exception e)
{
e.printStackTrace();
}
String html = "<html>" +
"<body>"+
"<img src=\"" + img_url.toString() + "\">" +
"</body>" +
"</html>";
JEditorPane swingbox = new JEditorPane();
swingbox.setEditorKit(new HTMLEditorKit());
swingbox.setContentType("text/html");
swingbox.setText(html);
swingbox.getDocument().putProperty("imageCache", image_cache);
JFrame frame=new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(swingbox);
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
推荐答案
问题在于您的代码:
swingbox.getDocument().putProperty("imageCache", image_cache);
注释掉这一行,它应该可以正常工作.经过一番挖掘,我发现问题出在 image_cache.put(img_url.toURI(), img) 上.它应该是 image_cache.put(img_url, img)
Comment out this line and it should work fine. After a bit of digging I found the problem was with image_cache.put(img_url.toURI(), img). It should have been image_cache.put(img_url, img)
自定义图像缓存可能会帮助您稍后调试代码.这是一个对我有用的更改示例.创建一个 ImageCache 类并使其在调用 get 时从缓存中返回图像(如果找到)或创建图像,则放入缓存并返回(如果没有)找到了.
A custom image cache may help you later on to debug the code. Here is an example with a bit of change that worked for me. Create an ImageCache class and make it so that when the get is called either the image is returned from cache if found or the image is created, put in cache and returned if not found.
示例代码:
public class TestClass {
private static ImageCache image_cache;
public static void main(String[] args) {
URL img_url = null;
image_cache = new ImageCache();
try
{
img_url = new File("C:/Users/User/Images/image.png").toURI().toURL();
Image img = Toolkit.getDefaultToolkit ().createImage (img_url);
image_cache.put(img_url, img);
}
catch (Exception e)
{
e.printStackTrace();
}
String html = "<html>" +
"<body>"+
"<img src=\"" + img_url.toString() + "\">" +
"</body>" +
"</html>";
JEditorPane swingbox = new JEditorPane ();
swingbox.setEditorKit(new HTMLEditorKit());
swingbox.setContentType("text/html");
swingbox.setText(html);
JFrame frame=new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(swingbox);
Dictionary cache=(Dictionary)swingbox.getDocument().getProperty("imageCache");
// put the cache if it is not present. it should be null in the beginning
if (cache==null) {
swingbox.getDocument().putProperty("imageCache",image_cache);
}
frame.setSize(800,600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
static class ImageCache extends Hashtable {
public Object get(Object key) {
Object result = super.get(key);
if (result == null){
result = Toolkit.getDefaultToolkit().createImage((URL) key);
put(key, result);
}
return result;
}
}
}
这篇关于Java JEditorPane 不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!