swing-使组件在JPanel上单击 [英] swing - get component clicked on JPanel

查看:124
本文介绍了swing-使组件在JPanel上单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个带有GridLayout的JPanel.然后,网格的每个单元格都包含另一个JPanel.

OK so I've got a JPanel with a GridLayout. Each cell of the grid then contains another JPanel.

我想做的是在下方" JPanel上有一个侦听器,然后告诉我单击了叠加" JPanel中的哪个-这样我就可以对它和周围的JPanel做出反应,而无需JPPanels知道他们的位置(他们改变了!)

What I'd like to be able to do is have a listener on the "underneath" JPanel which then tells me which of the "overlayed" JPanels was clicked - so I can react to it and the surrounding ones, without making the covering JPanels aware of their position (they change!!)

有没有办法做到这一点-类似于确定在MouseListener中单击了JPanel组件.事件处理,但我找不到将组件放在顶部的方法.

Is there a way of doing this - similar to Determine clicked JPanel component in the MouseListener. Event handling but I couldn't find a way of grabbing the component on top.

我可能会抓住这些共同的牧师并使用这些信息来解决-但我宁愿不这样做!

I could probably grab the co-oridnates and work it out using that info - but I'd rather not!!

任何帮助/指针/提示都将不胜感激:D

Any help/pointers/tips would be appreciated :D

推荐答案

执行相同的操作,但在源代码上使用getParent().或者,您可以在层次结构中进行更深入的搜索,甚至可以找到一些帮助方法: javax.swing.SwingUtilities.getAncestorOfClassgetAncestorNamed

Do the same thing but use getParent() on the source. Or you can search up the hierarchy if it is deeper, even some helper methods for that: javax.swing.SwingUtilities.getAncestorOfClass and getAncestorNamed

这篇关于swing-使组件在JPanel上单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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