[英]Java: MouseEvent on transparent JPanel
I have a LayeredPane
with two JPanel
s, all in a JFrame
. 我有一个带有两个
JPanel
的LayeredPane
,都在JFrame
。 Both JPanel
s are set to be transparent with setOpaque(false)
. 两个
JPanel
都设置为setOpaque(false)
透明。 However, I would like to capture mouse events on the (top, if it makes a difference) transparent panel. 但是,我想在(顶部,如果它有所不同)透明面板上捕获鼠标事件。 Is this possible, or should I just do it from the underlying
JFrame
? 这是可能的,还是我应该从底层的
JFrame
做到这一点? (It would definitely work to capture from the JFrame
; it just makes more logical sense to capture the events from the transparent frame). (肯定会从
JFrame
捕获;从透明帧中捕获事件更具逻辑意义)。
You can capture mouse events on whichever JPanel has a MouseListener attached to it, and is not encumbered by components that also have MouseListeners added, and who are laying on or above the original JPanel. 您可以在任何JPanel附加了MouseListener的情况下捕获鼠标事件,并且不会被添加了MouseListener的组件以及放置在原始JPanel上或上方的组件所阻碍。 This looks like a situation perfect for creating a small test class that is the most simple of GUI's, that has none of the bells and whistles of your GUI, but that illustrates your problem and hopeful solution.
这看起来非常适合创建一个最简单的GUI的小测试类,它没有GUI的花俏,但这说明了你的问题和有希望的解决方案。
Also, please know that the transparency does not play into any of this at all except as a distraction. 另外,请注意透明度除了作为分散注意力之外根本不会发挥任何透明度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.