0

私はこれを数時間試みており、Webも検索しましたが、解決策が見つかりません。

HTMLを読み取ってフォーマットできるテキストエリア(JTextArea、JTextPane、JEditorPaneのいずれでも構いません)を作成する必要があります。

JEdi​​torPaneがハイパーリンクを指定することで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);
        }
    }

ヘルプ?

4

1 に答える 1

0
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();
            }
        });
    }
}

このバージョンでは、(ほとんど)スニペット(実際のコンテンツを含む)を使用してHTMLを作成し、接頭辞を付けて<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();
            }
        });
    }
}
于 2012-06-13T17:43:09.993 に答える