使用带有HTMLEditorKit的JTextPane显示HTML图像

时间:2016-01-29 10:31:06

标签: java image swing jtextpane htmleditorkit

出于测试目的,我试图使用以下代码在每行文档旁边添加一张小图片:

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/radioGroup">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"

            android:layout_marginLeft="5dp"/>

        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"

            android:layout_marginRight="5dp"    />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"

            android:layout_marginLeft="5dp"/>

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New RadioButton"

            android:layout_marginRight="5dp"/>
    </LinearLayout>
</RadioGroup>

我的问题是,我所看到的只是这个,没有图片只是它的框架: enter image description here

我可能有错误的路径,但这应该是我所知道的正确: enter image description here

这是我的方法代码:

kit.insertHTML(doc, doc.getLength(), "<IMG SRC=file://res/picture.png>", 0, 0, HTML.Tag.IMG);

有谁知道为什么我能看到它的画面的小框架,我忘记了什么吗?提前致谢!如果您需要更多代码,请告诉我!

1 个答案:

答案 0 :(得分:1)

好的,我这样做了:

    String filename = getClass().getClassLoader().getResource("res/Kappa.png").toString();
    String preTag="<PRE>filename is : "+filename+"</PRE>";
    String imageTag="<img src=\""+filename+"\"/>";
    kit.insertHTML(doc, doc.getLength(), preTag+imageTag, 0, 0, HTML.Tag.IMG);

希望如果您遇到同样的问题,这可以帮助您:)