簡體   English   中英

如何在JEditorPane中使用charset = windows-1252顯示html

[英]How to display html with charset=windows-1252 in JEditorPane

我從服務器顯示了html響應,我想在JEditorPane中顯示它。 但是響應將字符集設置為Windows-1252,這似乎不會導致任何html呈現。 (當我將其注釋掉時,它就很好了)。

因此,一種解決方案是在嘗試顯示它之前先將其解析出來,但是我想知道是否存在已知的錯誤或可以做其他避免編輯響應的措施。 謝謝!

如果您想嘗試一下(如果您注釋掉第三行,您會看到它顯示):

public static void main(String args [])
{
    String html = "<!DOCTYPE HTML PUBLIC \"-////W3C////DTD HTML 4.0 Transitional//EN\">" +
        "<HTML>" +
        "<HEAD>" +
        "<META http-equiv=Content-Type content=\"text/html; charset=windows-1252\">" +
        "</HEAD>" +
        "<BODY>" +
        "<P>Hello World</P>" +
        "</BODY>" +
        "</HTML>";
    JEditorPane editor = new JEditorPane("text/html", html);
    editor.setEditable(false);
    JScrollPane pane = new JScrollPane(editor);
    JFrame f = new JFrame("charset=windows-1252");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(pane);
    f.setSize(800, 600);
    f.setVisible(true);
}

有一個錯誤: 4695909:當HEAD部分中包含META標簽時,JEditorPane無法顯示HTML BODY

但是您可以使用指令忽略該META標簽:

JEditorPane editor = new JEditorPane();
editor.setContentType("text/html");
editor.getDocument().putProperty("IgnoreCharsetDirective", true);
editor.setText(html);
editor.setEditable(false);

JScrollPane pane = new JScrollPane(editor);
JFrame f = new JFrame("charset=windows-1252");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(pane);
f.setSize(800, 600);
f.setVisible(true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM