【问题标题】:Construct HTML Field?构造 HTML 字段?
【发布时间】:2012-06-13 17:17:46
【问题描述】:

我已经尝试了几个小时,也搜索了网络,但根本找不到解决方案。

我需要构建一个可以读取和格式化 HTML 的文本区域(无论是 JTextArea、JTextPane 还是 JEditorPane 都无所谓)。

我知道 JEditorPane 可以通过给它一个超链接来显示 HTML,但是如果我已经得到了 HTML 文本,并且只想显示它..?如果我使用 setText() 它只显示一个白色字段。里面什么都没有。

我得到的 HTML 文本来自一封电子邮件。我明白了,使用以下代码(只是其中的一个 sn-p)

            String subject = message[row].getSubject();
            String from = InternetAddress.toString(message[row].getFrom());
            StringBuilder body = new StringBuilder();
            Multipart mp = (Multipart) message[row].getContent();
            for(int i = 0; i < mp.getCount(); i++) {
                BodyPart bp = mp.getBodyPart(i);
                String disp = bp.getDisposition();
                if(disp != null && (disp.equals(BodyPart.ATTACHMENT))) {
                    // Do something
                } else {
                    body.append(bp.getContent());
                }
            }
            EmailContent ec = new EmailContent(new JFrame(),true,from,subject,body.toString());
        } catch (IOException ex) {
            Logger.getLogger(MailPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MessagingException ex) {
            Logger.getLogger(MailPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

帮助?

【问题讨论】:

  • "但是如果我已经获得了 HTML 文本,并且只想显示它怎么办?"当你尝试它时发生了什么?
  • 谁减去我的票?另外,我尝试阅读该内容,但并没有真正帮助。 JEdi​​torPane 设置为执行 text/html 而不是 text/plain。当我使用 setText(htmltext) 时,它只会给我一个空白的 JEditorPane。我什至检查了传递的字符串,它很好。我不明白。
  • @AndrewThompson 它只是显示了 HTML 文本。没有按应有的方式格式化或实际显示。
  • 这可能与内容类型或JEditorPane 是否可编辑有关。为了尽快获得更好的帮助,请发帖SSCCE

标签: java html multipart


【解决方案1】:
import javax.swing.*;

public class HTMLFromString {

    HTMLFromString() {
        JEditorPane jep = new JEditorPane();

        String html = "<html><body><h1>Title</h1><p>Paragraph..";
        // Important!
        jep.setContentType("text/html");
        jep.setText(html);

        JOptionPane.showMessageDialog(null, jep);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                new HTMLFromString();
            }
        });
    }
}

此版本使用(大部分)您的 sn-p(带有一些实际内容)创建一些 HTML,前缀为 &lt;html&gt; 以产生结果。

import javax.swing.*;

public class HTMLFromString {

    static String SNIPPET = 
            "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional" +
            "//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
            "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head>" +
            "<meta http-equiv=\"Content-Type\" content=\"text/html; " +
            "charset=Windows-1252\" /><title></title>" +
            "<style type=\"text/css\">" +
            "a, a:visited" +
            "{" +
            "  color: #406377;" +
            "}" +
            "</style>" + 
            "</head>" +
            "<body bgcolor=\"#ffffff\" style=\"background-color: #ffffff; " +
            "width: 600px; font-family: Arial, Verdana, Sans-serif; color: " +
            "#000; font-size: 14px; margin: 0px;\"><h1>Hi!</h1>";

    HTMLFromString() {
        JEditorPane jep = new JEditorPane();

        String html = "<html>" + SNIPPET;
        // Important!
        jep.setContentType("text/html");
        jep.setText(html);

        JOptionPane.showMessageDialog(null, jep);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                new HTMLFromString();
            }
        });
    }
}

【讨论】:

  • 我能发现的唯一区别是,它以 html 标签开头,而我的不是。我得到的 HTML 是从电子邮件中提取的,所以它看起来像这样:(sn-p)pastebin.com/Ls6w0A7b
  • 您的仍然有一个&lt;HTML&gt; 标签,因此仍然是有效的 HTML。 &lt;!DOCTYPE&gt; 标记是注释,指定 HTML 的版本和类型。
  • 这只是 sn-p 的开始吗?如图所示,它的格式不正确。是的,Swing 组件中的 HTML 格式需要前导 &lt;html&gt;。将在 Swing 中呈现为 HTML 的字符串不需要格式正确(上面代码中的字符串既无效也不格式正确),但它对某些事情很挑剔。
  • 是的,这只是开始。我想知道是否必须删除这部分 ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" rel="nofollow" target="_blank">w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 还是什么?
  • 在解析之前将 添加到该行似乎已经修复了一点。至少谢谢你。下次请少脾气<_>
猜你喜欢
  • 1970-01-01
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-14
  • 2015-04-28
  • 2017-01-23
  • 2021-04-02
相关资源
最近更新 更多