如何检查 jEditorPane 中的某个索引是否包含粗体、斜体或下划线的字符?

How do I check if a certain index in a jEditorPane contains a character that is bolded, italicized, or underlined?

我制作了一个将文本文档加载到 jEditorPane 中的程序。文档中的某些文本是粗体、斜体或下划线,我想知道如何检查文本中的特定索引是否包含这三个属性之一。例如,如果我将文本的第一个字符存储在一个变量中,如下所示:

char chr = jEditorPane1.getDocument().getText(0, 1).charAt(0);

然后如何检查 chr 中的字符是否为粗体、斜体或下划线?

StyledDocument doc = (StyledDocument)jEditorPane1.getDocument()
Element textElem = doc.getCharacterElement(offset);
StyleConstants.isBold(textElem.getAttributes());
StyleConstants.isItalic(textElem.getAttributes());

或者,如果插入符位于偏移量中,您可以从工具包中获取 InputAttributes。

AttributeSet attrs = ((StyledEditorKit)jEditorPane1.getEditorKit()).getInputAttributes();
StyleConstants.isBold(attrs);