问题描述
我现在想往jTextPane里添加图片,就和QQ聊天框似的。jTextPane.setContentType("text/html");例如:jTextPane里的内容为:怎么就不好使了我在这个时候的鼠标指针在【怎么】的后面,我想添加一个图片表情<imgsrc=http://cache.soso.com/img/img/e102.gif>但是添加的不是这怎么的后面而是在<html>的这个位置添加了一行图片标签ml>下面是例子:----------------------------------------------------------------------------<imgsrc="file:/D:/Project/XEditor/bin/org/vorld/xeditor/tools/tageditor/ico/02.GIF">ml><p><imgsrc="http://cache.soso.com/img/img/e100.gif">怎么<b>就不</b><fontcolor="#99ff66">好使了</font></p>-----------------------------------------------插入代码:StringBuffersb=newStringBuffer();sb.append(jTextPane.getText());intend=jTextPane.getSelectionStart();if(faceIco==null)return;sb.insert(end,"<imgsrc=http://cache.soso.com/img/img/e102.gif>");jTextPane.setText(sb.toString());jTextPane.updateUI();
解决方案
解决方案二:
importjava.awt.BorderLayout;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjavax.swing.JFrame;importjavax.swing.JTextPane;publicclassTestextendsJFrameimplementsMouseListener{privateJTextPanejTextPane;publicTest(){super();jTextPane=newJTextPane();getContentPane().add(jTextPane,BorderLayout.CENTER);jTextPane.setContentType("text/html");jTextPane.setText("qweqweqweqweqw");jTextPane.addMouseListener(this);this.setBounds(100,100,400,300);}publicvoidmouseClicked(MouseEvente){StringBuffersb=newStringBuffer();sb.append(jTextPane.getText());intend=jTextPane.getSelectionStart();inthtmlHead=sb.indexOf("<body>");//4是空格的数量。。。sb.insert(end+htmlHead+"<body>".length()+4,"<imgsrc=http://cache.soso.com/img/img/e102.gif>");System.out.println(end);System.out.println(sb);jTextPane.setText(sb.toString());jTextPane.updateUI();}publicstaticvoidmain(String[]args){newTest().setVisible(true);}publicvoidmouseEntered(MouseEvente){//TODOAuto-generatedmethodstub}publicvoidmouseExited(MouseEvente){//TODOAuto-generatedmethodstub}publicvoidmousePressed(MouseEvente){//TODOAuto-generatedmethodstub}publicvoidmouseReleased(MouseEvente){//TODOAuto-generatedmethodstub}}