如何获取屏幕上JTextArea中某个单词(或所选内容)的位置的点值? [英] How to get a point value for where a word (or selection) in a JTextArea is on the screen?

查看:77
本文介绍了如何获取屏幕上JTextArea中某个单词(或所选内容)的位置的点值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使JTextArea中的anyGivenWord成为可操作的".例如,将鼠标悬停在anyGivenWord上方时,会显示一个工具提示,但将鼠标悬停在JTextArea中的任何其他单词上时不会出现.由于您无法直接执行此操作,因此我在考虑将一个组件放置在JTextArea中每个anyGivenWord出现的位置,并在JTextArea的大小更改时重新放置它的位置,以便单击看起来像anyGivenWord的内容.实际上是在组件上单击(导致触发了某些侦听器).为此,我需要知道该单词出现在什么点(x,y),以便可以将组件放置在该点.

My goal is to make anyGivenWord in a JTextArea "actionable". For example, when you hover the mouse over anyGivenWord, a tooltip appears, but not when you hover the mouse over any other word in the JTextArea. Since you can't do this directly to my knowledge, I was thinking of placing a component over where every anyGivenWord appears in the JTextArea, and have it re-position itself when the JTextArea's size changes, so that clicking on what appears to be anyGivenWord is actually clicking on the component(resulting in some listener being triggered). To do this, I would need to know at what point(x,y) that word occurs, so I can place the component at that point.

我当时想我可以搜索JTextArea并选择anyGivenWord的实例,然后选择getSelectionStart()和getSelectionEnd(),但是我认为这些方法返回的int是所选第一个/最后一个字母的索引,例如,它在getText()返回的字符串中的位置.我需要的是x/y坐标.这可能吗?

I was thinking I could search through the JTextArea and select instances of anyGivenWord, and then getSelectionStart() and getSelectionEnd(), but I think the int those methods return is the index of the first/last letter selected, as in, its position in the string returned by getText(). What I need is the x/y coordinates. Is this possible?

如果没有...关于如何以更优雅的方式来做我想做的事情的任何建议?

If not...any suggestions for more elegant ways to do what I'm trying to do?

推荐答案

您可以将JEditorPane与HTML结合使用,并将超链接添加到任何给定的单词.阅读Swing教程中如何使用编辑器窗格入门的基本知识.

You can use a JEditorPane with HTML and add Hyperlinks to any given word. Read the section from the Swing tutorial on How to Use Editor Panes for the basics to get you started.

然后,您可以添加一个HyperlinkListener来响应单击该单词时的事件.阅读JEditorPane API以获取使用HyperlinkListener的示例.

You can then add a HyperlinkListener to respond to events when the word is clicked. Read the JEditorPane API for an example of using a HyperlinkListener.

例如,当您将鼠标悬停在anyGivenWord上方时,会出现一个工具提示,

For example, when you hover the mouse over anyGivenWord, a tooltip appears,

如果您不想使用超链接,则可以通过覆盖getToolTipText(...)方法来控制工具提示文本.此方法接收MouseEvent,因此您可以获取鼠标位置.然后,您可以使用JTextArea的viewToModel(...)方法将鼠标的偏移量移到Document中.然后检查Utilities.该类将帮助您获取单词的开始/结束偏移量,因此您可以使用getText(...)方法在当前鼠标位置提取单词.

If you don't want to use Hyperlinks then you could control the tooltip text by overriding the getToolTipText(...) method. This method receives the MouseEvent so you can get the mouse location. You can then use the viewToModel(...) method of the JTextArea to get the offset of the mouse into the Document. Then check out the Utilities. class which will help you get the start/end offsets of the word so you can use the getText(...) method to extract the word at the current mouse location.

这篇关于如何获取屏幕上JTextArea中某个单词(或所选内容)的位置的点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆