【发布时间】:2015-09-03 01:40:07
【问题描述】:
我正在实现一个将文本设置为jTextPane 的函数。所以当用户点击jTextPane中的某个词时,该词的定义应该显示在jTextArea中。我知道如何在jTextPane 和jTextArea 中显示文本。令我困扰的是,当我点击jTextPane 整个文本被选中而不是选择那个特定的单词 :'(。我对插入符号的位置做了一些研究,但我做不到完全明白。这是我得到的:
private void jTextPane1MouseClicked(java.awt.event.MouseEvent evt) {
try
{
StyledDocument doc=(StyledDocument) jTextPane1.getDocument();;
Element ele = doc.getCharacterElement(jTextPane1.viewToModel(evt.getPoint()));
AttributeSet as = ele.getAttributes();
/* Here after getting the word from jTextPane we print the definition
of that word in jTextArea... I got the code for this part */
}
}
如何只获取点击的单词??
【问题讨论】: