How to display bold text in only parts of JTextArea?

时光毁灭记忆、已成空白 提交于 2020-01-05 08:51:57

问题


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?

Also can the contents of JTextArea be saved as an RTF document?


回答1:


No. What you're looking for is JEditorPane

This supports HTML (3.2?) which will allow you to use <font> (and other older tags) to provide rich text.

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




回答2:


I believe you need a JTextPane or JEditorPane for that.




回答3:


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


来源:https://stackoverflow.com/questions/35397585/set-different-font-formats-in-jtextarea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!