【发布时间】:2016-01-30 03:36:40
【问题描述】:
出于测试目的,我尝试使用以下代码在文档的每一行旁边添加一张小图片:
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();
}
}
有谁知道为什么我只能看到图片的小框架,我是不是忘记了什么?提前致谢!如果您需要更多代码,请告诉我!
【问题讨论】:
标签: java image swing jtextpane htmleditorkit