请教高手,如何在JTextPane中根据一段插入图片的html代码显示效果?举个例子:
JTextPane jtp=new JTextPane();
jtp.setContentType("text/html");
jtp.setText("<html><body><img src='图片路径'></body></html>");
可是这样过后运行时jtp里面并没有显示图片效果。
我想说,如果我不想用HTMLEditorKit来设置,就用上面的html代码插入图片,具体怎么操作?求高手赐教!!!!

解决方案 »

  1.   

    可以啊....你的是图片的路径不对吧..
    import java.io.IOException;import javax.swing.JFrame;
    import javax.swing.JTextPane;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.Document;
    import javax.swing.text.html.HTMLDocument;/**
     *
     * @author Administrator
     */
    public class MyTextPane extends JTextPane{
        
        /** Creates a new instance of JTextPanel_N */
        StringBuffer temp=new StringBuffer();
        javax.swing.text.html.HTMLDocument hd;
        int tempnum=0;
        public MyTextPane() {
            super.setContentType("text/html");
            super.setEditable(false);
            hd=new HTMLDocument();
            hd=(HTMLDocument) super.getDocument();
           
            
        }
        /***/
        public void addText(String str) {
            
            if(tempnum==0) {
                try {
                    hd.setInnerHTML(hd.getDefaultRootElement(),str);
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                tempnum++;
            } else {
                try {
                    System.out.println("super.getDocument().getEndPosition().getOffset() is: "+super.getDocument().getEndPosition().getOffset());
                    hd.insertBeforeEnd(hd.getDefaultRootElement(),str);
                    super.setCaretPosition(super.getDocument().getLength());
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                
            }
            //temp.append(str);
            // super.setText(temp.toString());
        }
        public static void main(String[]args)
        {
         JFrame  f = new JFrame();
         f.setSize(200,300);
         MyTextPane m = new MyTextPane();
         m.setEditable(true);
         m.addText("<img src='http://tech.ccidnet.com/pub/attachment/2005/4/416329.png'>dfd");
         m.setBounds(200,200, 300, 300);
         f.add(m);
         f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
         f.setVisible(true);
        }
    }
      

  2.   

    我想说的是如果图片是本地的,也就是用上面的方法将本地的图片以html代码的形式显示在JTextPane中该如何操作??谢谢!