简体   繁体   English

在JTextPane中的插入符位置获取样式

[英]Get style at caret position in a JTextPane

I have a basic text editor that can style documents (bold, underline, italicize, underline, align left/right/center, color, font size/family), and that all works great. 我有一个基本的文本编辑器,可以对文档进行样式设置(粗体,下划线,斜体,下划线,左/右/中心对齐,颜色,字体大小/系列),并且所有这些都很好用。 The problem I'm having is that I want the style to set my buttons and boxes to the correct styling for where the caret is. 我遇到的问题是我希望样式将按钮和框设置为插入符号所在的正确样式。

Say, for example, I have the string 举例来说,我有一个字符串

the quick brown fox jumps over the lazy dog 快速的棕色狐狸 跳过了那只懒狗

When I click between the u and i in quick, I want my italicize button to be toggled on to indicate that the text where the caret is has the italicized styling. 当我快速在u和i之间单击时,我希望我的斜体按钮处于打开状态,以指示插入符号所在的文本具有斜体样式。

I'm using a JTextPane to hold the text, and a StyledEditorKit for the styling. 我正在使用JTextPane来保存文本,并使用StyledEditorKit进行样式设置。

Thanks. 谢谢。

I think what you are looking for, is 我认为您正在寻找的是

getCharacterAttributes method from the JTextPane JTextPane中的getCharacterAttributes方法

http://download.oracle.com/javase/7/docs/api/javax/swing/JTextPane.html#getCharacterAttributes () http://download.oracle.com/javase/7/docs/api/javax/swing/JTextPane.html#getCharacterAttributes ()

Ok, just for anyone still looking for and didnt find the answer : for example : 好的,仅针对仍在寻找但找不到答案的任何人:例如:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM