Java JTextPane更改所选文本的字体 [英] Java JTextPane Change Font of Selected Text
问题描述
我有一个JTextPane(或JEditorPane,我可以使用任何问题)。如何将所选区域的字体更改为特定字体?
I have a JTextPane (or JEditorPane, I can use either no problem). How can I change the font of a selected area to a specific font?
textpane.getSelectedText()。setFont()
不行。 (即使使用font-family)
textpane.getSelectedText().setFont()
won't work. (Even with font-family)
推荐答案
你可以只改变JTextPane的字体作为一个整体,它不做富文本。
在JEditorPane下面有一个文档
(显然也是JTextPane),你得到了持有 getDocument()
。如果可以的话,你想将它转换为 StyledDocument
,然后你可以对给定的运行执行类似 setCharacterAttributes
的操作字符。
There's a Document
underneath JEditorPane (and apparently JTextPane too), which you get a hold of with getDocument()
. You want to cast that to a StyledDocument
if you can, and then you can do things like setCharacterAttributes
to a given run of characters.
在 http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html 。
这篇关于Java JTextPane更改所选文本的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!