[英]Java JTextPane Change Font of Selected Text
I have a JTextPane (or JEditorPane, I can use either no problem). 我有一个JTextPane(或JEditorPane,我可以使用任何问题)。 How can I change the font of a selected area to a specific font?
如何将所选区域的字体更改为特定字体?
textpane.getSelectedText().setFont()
won't work. textpane.getSelectedText().setFont()
不起作用。 (Even with font-family) (即使使用font-family)
You can change JTextPane's font only as a whole, it doesn't do rich text. 您只能将JTextPane的字体作为一个整体更改,它不会执行富文本格式。
There's a Document
underneath JEditorPane (and apparently JTextPane too), which you get a hold of with getDocument()
. JEditorPane下面有一个
Document
(显然也是JTextPane),你可以使用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. 如果可以,您希望将其
StyledDocument
转换为StyledDocument
,然后您可以执行setCharacterAttributes
到给定的字符集。
There are some (hopefully) helpful examples in the Java tutorial at http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html . 在http://java.sun.com/docs/books/tutorial/uiswing/components/editorpane.html的Java教程中有一些(希望)有用的例子。
You can do this by using JTextPane. 您可以使用JTextPane执行此操作。 It is impossible to do this using JTextArea.
使用JTextArea是不可能的。 .
。 Here is a best example on how to use JTextPane.
这是关于如何使用JTextPane的最佳示例。
Sample: http://download.oracle.com/javase/tutorial/uiswing/components/editorpane.html 示例: http : //download.oracle.com/javase/tutorial/uiswing/components/editorpane.html
Code: http://download.oracle.com/javase/tutorial/uiswing/examples/components/TextSamplerDemoProject/src/components/TextSamplerDemo.java 代码: http : //download.oracle.com/javase/tutorial/uiswing/examples/components/TextSamplerDemoProject/src/components/TextSamplerDemo.java
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.