简体   繁体   English

如何仅在JTextArea的部分显示粗体文本?

[英]How to display bold text in only parts of JTextArea?

Can I alter the text of a JTextArea to bold (append text) and then back to normal and will it only display the bold text in bold and the rest as normal? 我可以将JTextArea的文本更改为粗体(附加文本),然后再恢复为普通文本,是否仅将粗体文本显示为粗体,其余部分正常显示?

Also can the contents of JTextArea be saved as an RTF document? 还可以将JTextArea的内容另存为RTF文档吗?

No. What you're looking for is JEditorPane 不,您要找的是JEdi​​torPane

This supports HTML (3.2?) which will allow you to use <font> (and other older tags) to provide rich text. 它支持HTML(3.2?),可让您使用<font> (和其他较早的标记)提供富文本。

JEditorPane textarea = new JEditorPane("text/html", "");
textarea.setText("Here is some <b>bold text</b>");

EDIT : According to the javadoc I referenced above, JEditorPane also supports limited RTF. 编辑 :根据我上面引用的javadoc,JEditorPane还支持有限的RTF。 Don't forget to change the MIME to text/rtf 不要忘记将MIME更改为text/rtf

我相信您需要一个JTextPane或JEditorPane

 textArea.setFont(textArea.getFont().deriveFont(Font.BOLD, textArea.getFont().getSize()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM