【问题标题】:Java : getting the proper StartOffset for multiple text color in JTextPaneJava:在 JTextPane 中为多种文本颜色获取正确的 StartOffset
【发布时间】:2012-06-14 09:44:50
【问题描述】:

我需要创建一个程序来接受字符串输入并突出显示我的字符串列表中的某些单词。

例子:

String MyList[] = {"Dog","Cat","Lion"};

Sample Input Text: I have a Dog and a Cat and a Lion . "

如果输入文本没有任何换行符或转义字符,我没有问题,但如果输入文本有换行符,这就是结果。

例子:

String MyList[] = {"Dog","Cat","Lion"}; 

示例输入文本:

I have a Dog 
and a Cat
and a Lion

注意:大写字符表示高亮字符

I have a DOG
and a cAT
and a liON

我的高亮示例代码:

for(String x : MyList)
{

int startOffset = inputText.indexOf(x);
doc.setCharacterAttributes  (startOffset,x.length,myStyle,false);


}

我猜问题是换行符或转义字符,但我找不到解决方法。

如果输入字符串有换行,我如何才能获得单词的正确起始偏移量?

有什么想法吗?

【问题讨论】:

    标签: java swing highlight jtextpane


    【解决方案1】:
    • AFAIK 这可能来自Document

    • DocumentModel 对应于 JTextComponents

    • 发给example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 2013-10-06
      • 1970-01-01
      • 2012-01-14
      • 2014-07-19
      相关资源
      最近更新 更多