[英]How to preserve style of texts in JTextPane while append strings
嗨,我有JTextPane,我想在不同的时间加载内容(带有字体信息的文本)。 附加文本将始终更改字体名称或大小或粗体或斜体。 当我插入新字符串(使用diff字体)时,TextPane总是丢失先前加载的文本的字体信息。 如何始终为文本窗格保留字体信息? 我也想插入图片! 我必须为此使用HtmlDocument吗? 任何想法或建议都是最欢迎的。 提前致谢!! 我的JTextPane代码是
textPane.setText("\n This is sample text editor ex");
styleDoc = textPane.getStyledDocument();
SimpleAttributeSet keyWord = new SimpleAttributeSet();
// set font information for new text
StyleConstants.setFontFamily(keyWord, fontName);
StyleConstants.setFontSize(keyWord, fontSize);
try {
styleDoc.insertString(0,
styleDoc.getText(0, styleDoc.getLength()), null);
styleDoc.insertString(styleDoc.getLength(), "ample", keyWord);
} catch (Exception e) {
e.printStackTrace();
}
快速解答,快速提问。
1)使用JTextPane.inserComponent或JTextPane.insertIcon插入图像。
2)使用StyledEditorKit追加样式文本。 请记住,您还可以使用HTMLDocument和HTMLEditorKit处理html内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.