拦截或委托具有重叠组件的事件 [英] Intercepting or delegating events with overlapping components
问题描述
我有两个 JPanels
大小相等,一个在另一个顶部。顶层用作拖动选择面板,另一个用于添加其他组件。我的问题是这些添加的组件的鼠标事件处理程序不会被触发,因为它们是由覆盖面板处理的。我仍然可以拖动这些添加组件的顶部,但仍然为底层组件启用了 mouseEntered
和 mouseExited
?
I have two JPanels
equal in size, one over the top of the other. The top layer serves as a drag selection panel, and the other one has other components added to it. My problem is that the mouse event handlers of these added components aren't triggered, because they are handled by the overlaying panel instead. How can I still drag over the top of these added components, but still have mouseEntered
and mouseExited
enabled for the underlaying components?
这是一个屏幕截图:
Here is a screenshot:
如您所见,选择矩形绘在覆盖的 JPanel
,但是好像我的鼠标无法通过这个面板看看下面是什么(寻找更好的解释方式)。
As you can see, the selection rectangle is painted on the overlaying JPanel
, but it's as if my mouse can't get through this panel to see what's underneath (in search of a better way to explain that).
推荐答案
不要使用覆盖面板。我最后一次发表了一个建议,你可以这样做。
Don't use an overlaying panel. I gave a suggestion in your last posting on how you might do this.
或者,如果你使用覆盖面板,那么只需使用它来绘制,面板会收听鼠标事件,然后在覆盖面板上调用重画。
Or, if you do use an overlaying panel, then just use it for drawing and have the underlying panel listen for the mouse events and then invoke the repainting on the overlaying panel.
编辑:
也许更好的方法是在单个组件上使用MouseListener,然后处理矩形的绘图,您可以使用全局事件侦听器来监听mousePressed,mouseDragged,mouseReleased事件。您需要检查每个事件的来源,以查看源是否是面板上的面板本身oa组件。您可以使用 SwingUtilities.isDescendingFrom(...)
来帮助进行第二次测试。
Maybe a better approach is to use a MouseListener on the individual components and then to handle the drawing of the rectangle you can use a Global Event Listener to listen for the mousePressed, mouseDragged, mouseReleased event. You would need to check the source of each event to see if the source was the panel itself oa component on the panel. You can use SwingUtilities.isDescendingFrom(...)
to help with this second test.
这篇关于拦截或委托具有重叠组件的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!