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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:00:33  来源:igfitidea点击:

How to display bold text in only parts of JTextArea?

javaswingjtextarea

提问by iTEgg

Can I alter the text of a JTextAreato 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 JTextAreabe saved as an RTF document?

也可以将内容JTextArea保存为RTF文件吗?

采纳答案by Matt

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. 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()));