Java 如何仅在 JTextArea 的一部分中显示粗体文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2713863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display bold text in only parts of JTextArea?
提问by iTEgg
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?
我可以将 a 的文本更改JTextArea
为粗体(附加文本)然后恢复正常,它是否只会以粗体显示粗体文本而其余部分正常显示?
Also can the contents of JTextArea
be saved as an RTF document?
也可以将内容JTextArea
保存为RTF文件吗?
采纳答案by Matt
No. What you're looking for is JEditorPane
不。你要找的是JEditorPane
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. Don't forget to change the MIME to text/rtf
编辑:根据我上面引用的 javadoc,JEditorPane 还支持有限的 RTF。不要忘记将 MIME 更改为text/rtf
回答by JRL
I believe you need a JTextPane or JEditorPanefor that.
我相信你需要一个JTextPane 或 JEditorPane。
回答by Stoliarenko Alexander
textArea.setFont(textArea.getFont().deriveFont(Font.BOLD, textArea.getFont().getSize()));