Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 构造HTML字段?_Java_Html_Multipart - Fatal编程技术网

Java 构造HTML字段?

Java 构造HTML字段?,java,html,multipart,Java,Html,Multipart,我已经尝试了几个小时了,也在网上搜索了一下,但就是找不到解决办法 我需要构造一个文本区域,不管它是JTextArea、JTextPane还是JEditorPane,哪一个可以读取和格式化HTML并不重要 我知道JEditorPane可以通过给它一个超链接来显示HTML,但是如果我已经得到了HTML文本,并且只想显示它会怎么样呢。。?如果我使用setText,它只显示一个白色字段。里面什么都没有 我得到的HTML文本来自一封电子邮件。我明白了,使用下面的代码只是它的一个片段

我已经尝试了几个小时了,也在网上搜索了一下,但就是找不到解决办法

我需要构造一个文本区域,不管它是JTextArea、JTextPane还是JEditorPane,哪一个可以读取和格式化HTML并不重要

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

我得到的HTML文本来自一封电子邮件。我明白了,使用下面的代码只是它的一个片段

            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,前缀为以生成结果

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文本,并且只想显示它怎么办。。?你试的时候发生了什么?谁投了我的票?而且,我试着读了一遍,但没有真正起到帮助。JEditorPane设置为执行text/html而不是text/plain。当我使用setTexthtmltext时,它只会给我一个空白窗格。我甚至检查了通过的字符串,它很好。我不明白。@AndrewThompson它只是显示了HTML文本。没有按预期的格式或实际显示任何内容。这可能与内容类型或JEditorPane是否可编辑有关。为了更快地获得更好的帮助,我可以发布一个。唯一的区别是,它以html标记开头,而我的标记不是。我得到的HTML是从一封电子邮件中提取出来的,所以看起来是这样的:你的代码片段仍然有一个标记,因此仍然是有效的HTML。标记是一个注释,指定HTML的版本和类型。这只是代码段的开始吗?如图所示,它的格式不好。是的,Swing组件中的HTML格式需要前导字符。在Swing中呈现为HTML的字符串不需要格式良好。上面代码中的字符串既不有效,也不格式良好,但它对某些事情很挑剔。是的,这只是开始。我想知道我是否必须删除这一部分或什么?在解析之前添加到行似乎已经修复了一点。至少谢谢你。下次请少些脾气
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();
            }
        });
    }
}