gpt4 book ai didi

Java JEditorPane 不显示图像

转载 作者:行者123 更新时间:2023-11-30 07:04:26 26 4
gpt4 key购买 nike

我无法获得 JEditorPane 以将 HTML img 标记呈现为图像。显示的只是一个占位符图形。下面是我的代码。提前致谢。

我看到的:

enter image description here

我的代码:

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)

自定义图像缓存可以帮助您稍后调试代码。这是一个对我有用的示例,其中包含一些更改。创建一个 ImageCache 类,以便在调用 get 时,如果找到则从缓存中返回图像,或者创建图像,将其放入缓存并在未找到时返回找到了。

示例代码:

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 不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27669781/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com