Java Swing:更改鼠标悬停时的背景颜色
我实现了一个简单的鼠标侦听器,其中只要鼠标进入组件(JPanel),背景颜色就会发生变化,并且只要鼠标离开,背景颜色就会恢复原样。这有一些问题:
- 有时,鼠标移动得太快,以致于 mouseExit 事件不会被触发
- 如果我的组件有子组件,当鼠标移动到子组件时,它会触发 mouseExit
- 如果我将鼠标快速移到子项上,mouseEnter 事件没有被触发,
我猜这对于 Swing 老手来说是一个简单的事件。关于如何解决这个问题有什么建议吗?我不想使用计时器之类的...
I've implemented a simple mouse listener where the background color changes whenever the mouse enters the component (a JPanel), and it reverts back whenever the mouse leaves. This has some problems:
- Sometimes the mouse moves so quick that the mouseExit event is not fired
- If my component has childs, when the mouse moves to the childs it triggers the mouseExit
- If I move the mouse over to the childs quickly, the mouseEnter event is not fired
I'm guessing this is an easy one for Swing veterans. Any suggestions on how to fix this? I'd love not to use timers and such...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
,但如果这是一个问题,那么您可以处理 mouseMoved 来重置背景。
使用以下测试,代码仅在离开组件边界时才会执行:
I've never seen this to happen, but if it is an issue then you can handle mouseMoved instead to reset the background.
Use the following test and the code will only be executed when you leave the components bounds:
有多种解决方案:
AWTEventListener
添加到默认的Toolkit
并筛选您感兴趣的事件。不幸的是,这需要安全权限。EventQueue
并过滤事件。这需要安全权限,无论如何,让小程序和 WebStart/JNLP 应用程序获得该权限。There are a number of solutions:
AWTEventListener
to the defaultToolkit
and filter through for the events you are interested in. This unfortunately requires a security permission.EventQueue
and filter events. This requires a security permission, put applets and WebStart/JNLP applications get that permission anyway.在容器上尝试了各种方法但没有成功后,我最终使用了计时器。我的容器包含已经需要鼠标侦听器的元素,这并没有帮助。
计时器方法还意味着我可以将更改延迟一小段时间。 (在我的例子中,我在树节点(容器)中显示其他按钮,并更改背景。)
在容器上的 mouseEntered() 上,有一个 计时器 被创建(如果还没有),每 260 毫秒重复一次。每次调用计时器时,它都会确定鼠标是否在容器内。如果是这样,它第一次发出鼠标悬停信号。如果不是,它会发出非鼠标悬停信号并停止计时器。
在 Scala 中,如下所示,其中对 entryExit() 的方法调用对鼠标是否悬停进行编码(其中具有相同值的多次调用没有影响):
After trying various approaches on a container, without success, I ended up using a Timer. It didn't help that my container contained elements that already needed mouse listeners on them.
The timer approach also meant that I could delay the change for a short time. (In my case, I show additional buttons in a tree node (a container), as well as changing the background.)
On a mouseEntered() on the container, a Timer is created (if not there already) which repeats every 260 milliseconds. On each call of the Timer, it determines whether the mouse is inside the container. If so, on the first time it signals mouse-over. If not, it signals non-mouse-over and stops the timer.
In Scala, this is as follows, where the method call to entryExit() encodes whether the mouse is over or not (where multiple calls with the same value have no affect):
我无法重现这种行为。请编辑您的问题以提供演示该问题的简短代码示例。
当我创建 JPanel 并在其中放入一些内容时,当鼠标移到 JPanel 的子组件上时,JPanel 不会获得 mouseExit。我猜你已经为孩子们添加了 MouseListeners。
I can't reproduce this behavior. Please edit your question to provide a short code sample that demonstrates the problem.
When I create a JPanel, and put something in it, the JPanel does not get mouseExit when the mouse moves over a child component of the JPanel. I'm guessing that you've added MouseListeners to the children.