为了测试目的,我试图在每一行文档旁边添加一张小图片,代码如下:
kit.insertHTML(doc, doc.getLength(), "<IMG SRC=file://res/picture.png>", 0, 0, HTML.Tag.IMG);我的问题是,我所看到的只有这个,没有一个画面只是一个框架:

我可能弄错了,但据我所知,这应该是正确的:

这是我的方法的更多代码:
public void addText(String text, boolean timestamp) {
long timeMS = System.currentTimeMillis();
Date instant = new Date(timeMS);
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String time = sdf.format(instant);
boolean shouldScroll = false;
try {
HTMLDocument doc = (HTMLDocument) getChat().getDocument();
HTMLEditorKit kit = (HTMLEditorKit) getChat().getEditorKit();
JScrollBar vsb = getChatScroller().getVerticalScrollBar();
BoundedRangeModel model = vsb.getModel();
if (model.getExtent() + model.getValue() == model.getMaximum())
shouldScroll = true;
kit.insertHTML(doc, doc.getLength(), timestamp ? time + ": " + text : text, 0, 0, null);
kit.insertHTML(doc, doc.getLength(), "<IMG SRC=file://res/picture.png>", 0, 0, HTML.Tag.IMG);
if (shouldScroll)
getChat().setCaretPosition(doc.getLength());
} catch (IOException | BadLocationException e) {
e.printStackTrace();
}
}有谁知道为什么我所能看到的都是这幅画的小框,我忘了什么吗?提前感谢!如果你需要更多密码,请告诉我!
发布于 2016-01-29 19:34:03
好吧,我是这样做的:
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);如果你有同样的问题,希望这对你有帮助:)
https://stackoverflow.com/questions/35082241
复制相似问题