gpt4 book ai didi

java - 使用 JTextPane 和 HTMLEditorKit 显示 HTML 图像

转载 作者:行者123 更新时间:2023-11-30 07:38:05 30 4
gpt4 key购买 nike

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

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

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

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

这是我的方法的更多代码:

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();
}
}

有谁知道为什么我只能看到图片的小框,我是不是忘记了什么?提前致谢!如果您需要更多代码,请告诉我!

最佳答案

好的,我是这样完成的:

    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);

如果您遇到同样的问题,希望这对您有所帮助:)

关于java - 使用 JTextPane 和 HTMLEditorKit 显示 HTML 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35082241/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com