SWT Canvas 是否提供用于鼠标移动/单击活动区域的工具?

发布于 2024-10-03 23:20:52 字数 161 浏览 1 评论 0原文

我正在使用 Java 和 SWT 创建一个应用程序,并在 Canvas 上生成一个工作区。我需要使画布上的某些区域(控件)在鼠标悬停或单击时触发事件。当然,这可以通过监听 MouseMoveEvent 并手动检查鼠标的位置来完成,但我想知道 SWT 是否提供了更简单的方法来做到这一点。

谢谢

I'm creating an application with Java and SWT, and have a workspace generated on a Canvas. I need to make certain areas (controls) on the canvas trigger an event when mouseover-ed or clicked. Of course this could be done by listening to the MouseMoveEvent and checking the location of the mouse manually, but I would like to know if SWT provides an easier way to do this.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开雨落又逢春i 2024-10-10 23:20:52

据我所知,没有任何设施可以自动执行此操作。您需要注册鼠标侦听器并调查 x & y 手动坐标。

另一种方法可能是将单个 Canvas 对象添加到封闭的 Canvas 上。这样,您就可以向各个控件添加侦听器。然而,这将使用更多的内存(因为底层对象有一个缓冲区,以及顶部的控件),并且重绘将更加消耗 CPU 资源。

我个人偏好第一种技术。

As far as I know, there is no facility to automate this. You will need to register mouse listeners and investigate the x & y coordinates manually.

A different approach might be adding individual Canvas objects onto an enclosing Canvas. This way, you could add listeners to the individual controls. This will, however, use more memory (as the underlying object has a buffer, as well as the control on top) and redrawing will be more CPU intensive.

My personal preference goes to the first technique.

痴骨ら 2024-10-10 23:20:52

谢谢你的回答,保罗。我最终为每个控件创建了一个 Rectangle 对象的 ArrayList。每当 mousemove 事件触发时,我都会循环遍历每个 Rectangle 并使用鼠标坐标调用 Rectangle.contains() 。事实证明,这个解决方案非常有条理。

就我而言,控制事件是同质的(它们都做相同的事情),但如果有人有多个控制操作,则可以使用 Rectangle.data 来包含 Command 对象。

Thanks for the answer, Paul. I finally created an ArrayList of Rectangle objects for each control. Whenever the mousemove event triggers, I loop through each Rectangle and call Rectangle.contains() with the mouse coordinates. This solution turned out to be very organized.

In my case, the control events are homogenous (they all do the same thing), but if anyone has multiple control actions, Rectangle.data can be used to contain a Command object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文