JAVA - set font of selectedtext in a JEditorPane

2019-07-19 05:34发布

问题:

How can I set the font/bold/italic/underline whatever of some text in a JEditorPane?

I am trying to create a simple document editor, but i can only figure out how to set the font/bold/italic/whatever of the ENTIRE JEditor/JText pane.

RTF is preferred, HTML is fine as well.

回答1:

JEditorPane.getDocument() returns an instance of StyledDocument. StyledDocument has setCharacterAttributes() method to set the font properties.

Related example: http://www.java2s.com/Code/JavaAPI/javax.swing/JTextPanesetCharacterAttributesAttributeSetattrbooleanreplace.htm