[英]Java - getting/setting line height of JTextPane with content type “text/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.