4

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

textpane.getSelectedText().setFont()不会工作。(即使使用字体系列)

4

2 回答 2

5

您只能整体更改 JTextPane 的字体,它不做富文本。

下面有一个DocumentJEditorPane(显然也有 JTextPane),您可以使用getDocument(). 如果可以的话,您想将其转换为 a StyledDocument,然后您可以setCharacterAttributes对给定的字符执行诸如此类的操作。

http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html的 Java 教程中有一些(希望是)有用的示例。

于 2009-12-15T06:19:15.333 回答
1

您可以通过使用 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

于 2011-08-17T11:33:47.570 回答