Java窗口窗格不显示图像

Java窗口窗格不显示图像,java,swing,jeditorpane,Java,Swing,Jeditorpane,我无法使用JEditorPane将HTML img标记呈现为图像。所有显示的都是占位符图形。下面是我的代码。提前谢谢 我看到: 我的代码: 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 {

我无法使用JEditorPane将HTML img标记呈现为图像。所有显示的都是占位符图形。下面是我的代码。提前谢谢

我看到:

我的代码:

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.putimg_url.toURI,img上。它应该是image\u cache.putimg\u 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;
        }
    }

}

我感到惊讶的是,实际的解决方案甚至更简单。您可以使用我提供的自定义ImageCache,但实际问题很愚蠢。它应该是image\u cache.putimg\u url,img而不是image\u cache.putimg\u url.toURI,img;;