Java JTextPane更改所选文本的字体

我有一个JTextPane(或JEditorPane,我可以使用任何问题)。 如何将所选区域的字体更改为特定字体?

textpane.getSelectedText().setFont()不起作用。 (即使使用font-family)

您只能将JTextPane的字体作为一个整体更改,它不会执行富文本。

JEdi​​torPane下面有一个Document (显然也是JTextPane),你可以使用getDocument() 。 如果可以,您希望将其StyledDocument转换为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

代码: http : //download.oracle.com/javase/tutorial/uiswing/examples/components/TextSamplerDemoProject/src/components/TextSamplerDemo.java