Java 使用pane.getDocument.insert()在JEditorPane中插入图像
我正在创建一个聊天应用程序,并希望将字符串附加到JEditorPane中,因此我使用JEditorPane.getDocument.insert()方法执行此操作:Java 使用pane.getDocument.insert()在JEditorPane中插入图像,java,image,swing,jeditorpane,Java,Image,Swing,Jeditorpane,我正在创建一个聊天应用程序,并希望将字符串附加到JEditorPane中,因此我使用JEditorPane.getDocument.insert()方法执行此操作: clientListDoc.insertString(clientListDoc.getLength(),image+"-"+name[0]+"\n", null); 但现在我还想显示图像。我已将内容类型设置为HTML,并使用以下方法: String temp=ClassLoader.getSystemResource("imag
clientListDoc.insertString(clientListDoc.getLength(),image+"-"+name[0]+"\n", null);
但现在我还想显示图像。我已将内容类型设置为HTML,并使用以下方法:
String temp=ClassLoader.getSystemResource("images/away.png").toString();
image="<img src='"+temp+"'></img>";
String temp=ClassLoader.getSystemResource(“images/away.png”).toString();
image=“”;
但是如果我使用insert(),我不会在JEditorPane上获得图像,但是当我使用SETEXT()时,图像会显示出来。请帮忙!!这两件事我都想做
我认为有一种方法可以是使用getText获取以前的字符串,并将新字符串附加到此字符串,然后使用setText()设置整个字符串,但是有更好的解决方案吗?使用setText()
方法,可以将其格式化为HTML。使用insertString
,您的标记将转换为文本。查看文档的源HTML,您将看到将<;img src=imagepath>
您需要使用HTMLDocument类来正确插入图像:
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JEditorPane edPane = new JEditorPane();
try {
edPane.setContentType("text/html");
System.out.println(edPane.getText());
HTMLEditorKit hek = new HTMLEditorKit();
edPane.setEditorKit(hek);
HTMLDocument doc = (HTMLDocument) edPane.getDocument();
doc.insertString(0, "Test testing", null);
Element[] roots = doc.getRootElements();
Element body = null;
for( int i = 0; i < roots[0].getElementCount(); i++ ) {
Element element = roots[0].getElement( i );
if( element.getAttributes().getAttribute( StyleConstants.NameAttribute ) == HTML.Tag.BODY ) {
body = element;
break;
}
}
doc.insertAfterEnd(body,"<img src="+ClassLoader.getSystemResource("thumbnail.png").toString()+">");
System.out.println(edPane.getText());
} catch(BadLocationException e) {
} catch (java.io.IOException e) {}
frame.add(edPane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
import javax.swing.*;
导入javax.swing.text.*;
导入javax.swing.text.html.*;
课堂测试{
公共静态void main(字符串[]args){
JFrame=新JFrame();
JEditorPane edPane=新的JEditorPane();
试一试{
setContentType(“text/html”);
System.out.println(edPane.getText());
HTMLEditorKit hek=新的HTMLEditorKit();
edPane.setEditorKit(hek);
HTMLDocument doc=(HTMLDocument)edPane.getDocument();
文档插入字符串(0,“测试”,空);
元素[]根=doc.getRootElements();
元素体=空;
对于(int i=0;i
使用setText()
方法,您可以将其格式化为HTML。使用insertString
,您的标记将转换为文本。查看文档的源HTML,您将看到将<;img src=imagepath>
您需要使用HTMLDocument类来正确插入图像:
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
JEditorPane edPane = new JEditorPane();
try {
edPane.setContentType("text/html");
System.out.println(edPane.getText());
HTMLEditorKit hek = new HTMLEditorKit();
edPane.setEditorKit(hek);
HTMLDocument doc = (HTMLDocument) edPane.getDocument();
doc.insertString(0, "Test testing", null);
Element[] roots = doc.getRootElements();
Element body = null;
for( int i = 0; i < roots[0].getElementCount(); i++ ) {
Element element = roots[0].getElement( i );
if( element.getAttributes().getAttribute( StyleConstants.NameAttribute ) == HTML.Tag.BODY ) {
body = element;
break;
}
}
doc.insertAfterEnd(body,"<img src="+ClassLoader.getSystemResource("thumbnail.png").toString()+">");
System.out.println(edPane.getText());
} catch(BadLocationException e) {
} catch (java.io.IOException e) {}
frame.add(edPane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
import javax.swing.*;
导入javax.swing.text.*;
导入javax.swing.text.html.*;
课堂测试{
公共静态void main(字符串[]args){
JFrame=新JFrame();
JEditorPane edPane=新的JEditorPane();
试一试{
setContentType(“text/html”);
System.out.println(edPane.getText());
HTMLEditorKit hek=新的HTMLEditorKit();
edPane.setEditorKit(hek);
HTMLDocument doc=(HTMLDocument)edPane.getDocument();
文档插入字符串(0,“测试”,空);
元素[]根=doc.getRootElements();
元素体=空;
对于(int i=0;i
用内联css或html格式化。你太棒了!我很高兴:)!用内联css或html格式化。你太棒了!我很高兴:)!