簡體   English   中英

如何在Java中使用HTML內容類型在JTextPane中打印HTML標記

[英]How to print an HTML tag in a JTextPane with html content type in Java

我正在使用Java創建XML編輯器,並且正在使用JTextPanes來顯示XML的內容。 JTextPanes的內容類型是"text/html"因此它跳過了我要顯示在窗格中的所有XML標記。 這是大多數JTextPane類:

public class Label extends JTextPane {
    private static final long serialVersionUID = 6151945111760925061L;

    public Label(String text) {
        setContentType("text/html");
        setText(StringOperations.toHtml(text));
        setEditable(false);
        setBackground(null);
        setBorder(null);
        setFont(new Font("Eras Bold ITC", Font.PLAIN, 11));
    }

}

這是將純文本轉換為html的方法。

public static String toHtml(String text) {
    return ("<html>" + text + "</html>");
}

例如,當我插入: "<resource>4</resource>"

輸出為"4"

我想要的輸出是"<resource>4</resource>"

我試着做:

return ("<html>" + text.replace("<","/<") + "</html>");
return ("<html>" + text.replace("<","\"<\"") + "</html>");

但是標簽仍然不可讀。

您能告訴我如何轉義'<''>'字符嗎?

需要創建自定義EditorKit並將其設置為JTextPane,

例如 :

https://www.boplicity.nl/knowledgebase/Java/Xml+syntax+highlighting+in+Swing+JTextPane.html

暫無
暫無

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

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