简体   繁体   English

如何在Java中使用HTML内容类型在JTextPane中打印HTML标记

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

I am creating an XML editor with Java and I am using JTextPanes to show the content of the XML . 我正在使用Java创建XML编辑器,并且正在使用JTextPanes来显示XML的内容。 The content type of the JTextPanes is "text/html" so it is skipping all of the XML tags that I want to appear in the pane. JTextPanes的内容类型是"text/html"因此它跳过了我要显示在窗格中的所有XML标记。 Here is most of the JTextPane class: 这是大多数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));
    }

}

Here is the method that converts the plain text to html. 这是将纯文本转换为html的方法。

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

So for example when I insert: "<resource>4</resource>" 例如,当我插入: "<resource>4</resource>"

The output is "4" . 输出为"4"

What I want is for the output to be "<resource>4</resource>" . 我想要的输出是"<resource>4</resource>"

I tried doing: 我试着做:

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

But the tags are still unreadable. 但是标签仍然不可读。

Could you tell me how can I escape the '<' and '>' characters? 您能告诉我如何转义'<''>'字符吗?

Need to create custom EditorKit and set it to JTextPane, 需要创建自定义EditorKit并将其设置为JTextPane,

for example : 例如 :

https://www.boplicity.nl/knowledgebase/Java/Xml+syntax+highlighting+in+Swing+JTextPane.html 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