首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JTextpane不显示HTMLEditorkit和HTMLDocument(坏图标)的照片

JTextpane不显示HTMLEditorkit和HTMLDocument(坏图标)的照片
EN

Stack Overflow用户
提问于 2019-04-06 11:37:59
回答 1查看 40关注 0票数 0

使用HTMLEditorKit andHTMLDocument的JText窗格不会呈现照片,并且在保存文件时呈现一个破损的图标,我发现照片被加载到HTMLEditorKit中

代码语言:javascript
复制
try {
                    File f =im.file;
                    icon = new ImageIcon(f.getName());

                    int w = 300;
int h =300;
                    MutableAttributeSet attr = new SimpleAttributeSet();
attr.addAttribute(StyleConstants.NameAttribute,
HTML.Tag.IMG);
attr.addAttribute(HTML.Attribute.SRC, f.getAbsolutePath());
attr.addAttribute(HTML.Attribute.HEIGHT,
Integer.toString(h));
attr.addAttribute(HTML.Attribute.WIDTH,
Integer.toString(w));
int p = editor.getCaretPosition();
doc.insertString(p, " ", attr);

                   HTMLDocument docx = (HTMLDocument)editor.getDocument();
                    HTMLEditorKit kit=(HTMLEditorKit)editor.getEditorKit();
         String filename = f.getAbsolutePath();
    String preTag="<PRE>filename is : "+filename+"</PRE>";
    String imageTag="<img src=\""+filename+"\"/>";
    String s ="<html><img src="+filename+"></img></html>";



 // kit.insertHTML(doc, doc.getLength(), s, 0, 0, HTML.Tag.IMG);


         doc.insertString(editor.getCaretPosition(), "\r\n", editor.getInputAttributes());



                }
````[see the broken icon[\]\[1\][1]
代码语言:javascript
复制
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-07 06:59:20

下面是如何使用file.toURI.toURL().toString();`完成的

代码语言:javascript
复制
try {
                    File f =im.file;
                    icon =(ImageIcon) label.getIcon();
             String filename= f.toURI().toString();  
                    int w =icon.getIconWidth();
int h =icon.getIconHeight();

                    MutableAttributeSet attr = new SimpleAttributeSet();
attr.addAttribute(StyleConstants.NameAttribute,HTML.Tag.IMG);
attr.addAttribute(HTML.Attribute.SRC, filename);
attr.addAttribute(HTML.Attribute.HEIGHT,
Integer.toString(h));
attr.addAttribute(HTML.Attribute.WIDTH,
Integer.toString(w));
int p = editor.getCaretPosition();
doc.insertString(p, " ", attr);
 doc.insertString(editor.getCaretPosition(), "\r\n", editor.getCharacterAttributes());







                } catch (Exception ex) {
                    }

        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55548695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档