Java 使用带有HTMLEditorKit的JTextPane显示HTML图像
出于测试目的,我尝试在文档的每一行旁边添加一个小图片,代码如下:Java 使用带有HTMLEditorKit的JTextPane显示HTML图像,java,image,swing,jtextpane,htmleditorkit,Java,Image,Swing,Jtextpane,Htmleditorkit,出于测试目的,我尝试在文档的每一行旁边添加一个小图片,代码如下: kit.insertHTML(doc, doc.getLength(), "<IMG SRC=file://res/picture.png>", 0, 0, HTML.Tag.IMG); kit.insertHTML(doc,doc.getLength(),“”,0,0,HTML.Tag.IMG); 我的问题是,我看到的只是这个,没有图片,只有一个相框: 我可能走错了路,但据我所知,这应该是正确的: 这是我的方
kit.insertHTML(doc, doc.getLength(), "<IMG SRC=file://res/picture.png>", 0, 0, HTML.Tag.IMG);
kit.insertHTML(doc,doc.getLength(),“”,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();
}
}
public void addText(字符串文本,布尔时间戳){
long-timeMS=System.currentTimeMillis();
日期瞬间=新日期(timeMS);
SimpleDataFormat sdf=新的SimpleDataFormat(“HH:mm”);
字符串时间=sdf.format(即时);
布尔值shouldcoll=false;
试一试{
HTMLDocument doc=(HTMLDocument)getChat().getDocument();
HTMLEditorKit=(HTMLEditorKit)getChat().getEditorKit();
JScrollBar vsb=getChatScroller().getVerticalScrollBar();
BoundedRangeModel model=vsb.getModel();
if(model.getExtent()+model.getValue()==model.getMaximum())
shouldcoll=true;
insertHTML(doc,doc.getLength(),时间戳?time+“:”+文本:文本,0,0,null);
insertHTML(doc,doc.getLength(),“”,0,0,HTML.Tag.IMG);
如果(应滚动)
getChat().setCaretPosition(doc.getLength());
}捕获(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);
String filename=getClass().getClassLoader().getResource(“res/Kappa.png”).toString();
String preTag=“文件名为:“+filename+”;
字符串imageTag=“”;
insertHTML(doc,doc.getLength(),preTag+imageTag,0,0,HTML.Tag.IMG);
如果您有同样的问题,希望这对您有所帮助:)