![](/img/trans.png)
[英]Caret position in JTextPane is not correct ?! Bug or expected behavior?
[英]Get style at caret position in a JTextPane
我有一個基本的文本編輯器,可以對文檔進行樣式設置(粗體,下划線,斜體,下划線,左/右/中心對齊,顏色,字體大小/系列),並且所有這些都很好用。 我遇到的問題是我希望樣式將按鈕和框設置為插入符號所在的正確樣式。
舉例來說,我有一個字符串
快速的棕色狐狸 跳過了那只懶狗
當我快速在u和i之間單擊時,我希望我的斜體按鈕處於打開狀態,以指示插入符號所在的文本具有斜體樣式。
我正在使用JTextPane來保存文本,並使用StyledEditorKit進行樣式設置。
謝謝。
我認為您正在尋找的是
JTextPane中的getCharacterAttributes
方法
http://download.oracle.com/javase/7/docs/api/javax/swing/JTextPane.html#getCharacterAttributes ()
好的,僅針對仍在尋找但找不到答案的任何人:例如:
AttributeSet attributeSet = textPane.getCharacterAttributes();
Object bold = attributeSet == null ? null : attributeSet.getAttribute( StyleConstants.Bold );
System.out.println( "Bold : " + bold );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.