首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >HTML Base64图像的JEditorPane内容类型

HTML Base64图像的JEditorPane内容类型
EN

Stack Overflow用户
提问于 2018-06-29 22:12:51
回答 1查看 1.2K关注 0票数 1

我正在使用JeditorPane和JEditorKit来显示一些超文本标记语言。HTML显示正确,但图像显示为损坏(它们在浏览器中显示正常)。图像源是base64。我这样设置内容类型:

代码语言:javascript
复制
final JEditorPane ed=new JEditorPane();
ed.setContentType("text/html");

我猜是因为它同时包含文本和图像,所以内容类型不正确。如果是这样的话,它应该设置为什么?蒂娅。

**马杜山·佩雷拉回复后**

代码语言:javascript
复制
final JEditorPane ed=new JEditorPane();
ed.setContentType("text/html");
ed.setEditable(false);
HTMLDocument html=(HTMLDocument) ed.getDocument();
html.putProperty("IgnoreCharsetDirective", new Boolean(true));
HTMLEditorKit htmle=(HTMLEditorKit) ed.getEditorKit();
try {
    htmle.insertHTML(html,html.getLength(),content,0,0,null);
} catch (BadLocationException | IOException e) {
    // Should not get here
    e.printStackTrace();
}
ed.addHyperlinkListener(new HyperlinkListener() {
          public void hyperlinkUpdate(final HyperlinkEvent pE) {
              if (HyperlinkEvent.EventType.ACTIVATED == pE.getEventType()) {
                  String desc = pE.getDescription();
                  if (desc == null || !desc.startsWith("#")) return;
                  desc = desc.substring(1);
                  ed.scrollToReference(desc);
              }
          }
      });
ed.setCaretPosition(0);
JScrollPane scroll=new JScrollPane(ed,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JPanel jp=new JPanel();
Dimension size=new Dimension(700,700);
jp.setPreferredSize(size);
jp.setLayout(new BorderLayout());
jp.add(scroll);
        JOptionPane.showMessageDialog(null,jp,title,JOptionPane.INFORMATION_MESSAGE);

“HTML”是包含HTML的字符串。它是使用IOUtils.toString从html文件类型中读取的。我可能不得不开发一个SCCE。

实现CustomEditor

代码语言:javascript
复制
        final JEditorPane ed=new JEditorPane();
        ed.setContentType("text/html");
        ed.setEditable(false);
        CustomToolKit htmle=new CustomToolKit();
        ed.setEditorKit(htmle);
        String content=readFile(fileName_+".html").replaceAll("(\\r|\\n)", "");
        content=content.replace("!!!!",VERSION.VERSION);
        ed.setText(content);
        ed.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(final HyperlinkEvent pE) {
                if (HyperlinkEvent.EventType.ACTIVATED == pE.getEventType()) {
                    String desc = pE.getDescription();
                    if (desc == null || !desc.startsWith("#")) return;
                    desc = desc.substring(1);
                    ed.scrollToReference(desc);
                }
            }
        });
        ed.setCaretPosition(0);
        JScrollPane scroll=new JScrollPane(ed,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        JPanel jp=new JPanel();
        Dimension size=new Dimension(700,700);
        jp.setPreferredSize(size);
        jp.setLayout(new BorderLayout());
        jp.add(scroll);
        JOptionPane.showMessageDialog(null,jp,title,JOptionPane.INFORMATION_MESSAGE);
    }

现在我什么也得不到。显然,我没有正确地执行您的建议。

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

https://stackoverflow.com/questions/51103717

复制
相关文章

相似问题

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