在jeditorpane上显示图像(java swing) [英] showing images on jeditorpane (java swing)

查看:360
本文介绍了在jeditorpane上显示图像(java swing)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以这种方式创建的JEditorPane:

I have a JEditorPane created by this way:

JEditorPane pane = new JEditorPane("text/html", "<font face='Arial'>" + my_text_to_show + "<img src='/root/img.gif'/>" + "</font>");

我将此窗格放在JFrame上。

I put this pane on a JFrame.

文字显示正确,但我看不到图片,只有一个方块表示应该有一张图片(即:图片未找到时浏览器显示的图片)

Text is shown correctly, but I can't see the picture, there is only a square indicating that there should be an image (i.e.: "broken image" shown by browsers when picture has not been found)

推荐答案

您必须提供类型并获取资源。就这样。我测试的例子,但我不确定格式化。希望它有所帮助:

You have to provide type, and get the resource. That's all. My tested example, but I'm not sure about formating. Hope it helps:

import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.JFrame;

public class Test extends JFrame {

    public static void main(String[] args) throws Exception {
        Test.createAndShowGUI();
    }

    private static void createAndShowGUI() throws IOException {

        JFrame.setDefaultLookAndFeelDecorated(true); 

        JFrame frame = new JFrame("HelloWorldSwing");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        String imgsrc = 
            Test.class.getClassLoader().getSystemResource("a.jpg").toString();
        frame.getContentPane().add(new JEditorPane("text/html",
            "<html><img src='"+imgsrc+"' width=200height=200></img>"));
        frame.pack();

        frame.setVisible(true);
    }
}

这篇关于在jeditorpane上显示图像(java swing)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆