问题描述
我必须编写一个程序,我必须在JTextarea中突出显示该句子。
我有一个名为原始文件和摘要文件的文件。原始文件将显示在textarea框中,我必须在textarea框中突出显示摘要文件的所有句子。
I have to write a program in which i have to highlight the sentence in JTextarea.I have one file called original file and summary file. Original file will be displayed in textarea box and i have to highlight all the sentences of a summary file in textarea box.
例如。如果原始文件包含Tendulkar出生在孟买(现孟买)。他的母亲Rajni在保险业工作,他的父亲Ramesh Tendulkar,一位马拉地语小说家,在他最喜欢的音乐总监Sachin Dev Burman之后命名为Tendulkar.Tendulkar's哥哥Ajit鼓励他打板球.Tendulkar还有另外两个兄弟姐妹:兄弟Nitin和妹妹Savita。
For example. If original file contains "Tendulkar was born in Bombay (now Mumbai). His mother Rajni worked in the insurance industry,and his father Ramesh Tendulkar, a Marathi novelist, named Tendulkar after his favourite music director, Sachin Dev Burman. Tendulkar's elder brother Ajit encouraged him to play cricket. Tendulkar has two other siblings: a brother Nitin, and sister Savita."
上面的文字显示在textarea中。
Above text is displayed in textarea.
在摘要文件中我有Tendulkar出生在孟买(现孟买).Tendulkar还有另外两个兄弟姐妹:兄弟Nitin和妹妹Savita。 / strong>
In summary file i have "Tendulkar was born in Bombay (now Mumbai). Tendulkar has two other siblings: a brother Nitin, and sister Savita."
我想在textarea中突出显示这些句子。请有人能告诉我该怎么做?
提前谢谢:)
I want these sentences to be highlighted in textarea. Please can anyone tell me how can i do this?Thank you in advance :)
推荐答案
您可以使用:
You can use a Highlighter
:
Highlighter h = textArea.getHighlighter();
h.removeAllHighlights();
int pos = originalText.indexOf(summaryText, 0);
h.addHighlight(pos ,
pos + summaryText.Length,
DefaultHighlighter.DefaultPainter);
这篇关于突出显示textarea中的句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!