簡體   English   中英

Java JTextPane更改所選文本的字體

[英]Java JTextPane Change Font of Selected Text

我有一個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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM