我有一个 JTextPane(或 JEditorPane,我可以使用任何一个都没有问题)。如何将选定区域的字体更改为特定字体?
textpane.getSelectedText().setFont()
不会工作。(即使使用字体系列)
您只能整体更改 JTextPane 的字体,它不做富文本。
下面有一个Document
JEditorPane(显然也有 JTextPane),您可以使用getDocument()
. 如果可以的话,您想将其转换为 a StyledDocument
,然后您可以setCharacterAttributes
对给定的字符执行诸如此类的操作。
在http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html的 Java 教程中有一些(希望是)有用的示例。
您可以通过使用 JTextPane 来做到这一点。使用 JTextArea 是不可能做到这一点的。. 这是有关如何使用 JTextPane 的最佳示例。
示例:http: //download.oracle.com/javase/tutorial/uiswing/components/editorpane.html