简体   繁体   English

当子组件中添加了其他MouseListener时,父组件的MouseListener无法在子组件内部运行

[英]MouseListener of parent component is not working inside child component when other MouseListener added in child

I have a JPanel parent , and a JPanel child . 我有一个JPanel parent ,还有一个JPanel child I have added a MouseListener in parent and another in child. 我在父级中添加了一个MouseListener,在子级中添加了另一个。 Here is the code: 这是代码:

public void init(MouseListener listenerForParent, MouseListener listenerForChild)
{
    JPanel parent = new JPanel("Parent");
    JPanel child = new JPanel("Child");
    parent.add(child);
    parent.addMouseListener(listenerForParent);
    ...
}

Everything is working fine upto now. 到现在为止一切正常。 The mouseClicked(...) of listenerForParent is calling when I clicked I am receiving events for above code, even if I click on child JPanel, the mouseClicked(...) of listenerForParent is called. 当我单击时,将调用listenerForParentmouseClicked(...) ,我正在接收上述代码的事件,即使单击child JPanel,也会调用listenerForParentmouseClicked(...)
But problem is here. 但是问题就在这里。 When I am adding Listener in child JPanel : 当我在child JPanel中添加Listener时:

public void init(MouseListener listenerForParent, MouseListener listenerForChild)
{
    JPanel parent = new JPanel("Parent");
    ...
    parent.addMouseListener(listenerForParent);
    child.addMouseListener(listenerForChild); // added new line i.e. adding MouseListener in child
}

Now when I click the child JPanel area then only the mouseClicked(...) of listenerForChild is calling. 现在,当我单击child JPanel区域时,仅在调用listenerForChildmouseClicked(...)
Is it possible to get event in both listeners. 是否有可能在两个侦听器中都获得事件。

Where, I cannot modify the given MouseListeners. 在哪里,我无法修改给定的MouseListeners。 ie I cannot change mouseClicked(...) because It is given to me by some other class whose source I don't have. 即我不能更改mouseClicked(...)因为它是由其他我没有来源的类提供给我的。 Therefore I cannot use dispatchEvent(AWTEvent e) method. 因此,我不能使用dispatchEvent(AWTEvent e)方法。
Thanks in advance. 提前致谢。

You cannot modify it, but you can wrap it. 您不能修改它,但是可以包装它。 You could put it into your own local implementation of a MouseListener and do what you have to do and then call the passed-in mouse listener. 您可以将其放入您自己的MouseListener的本地实现中,执行所需的操作,然后调用传入的鼠标侦听器。

EDIT: 编辑:

Right now you are doing this: 现在,您正在执行此操作:

parent.addMouseListener(listenerForParent);

but as well you could: 但是您也可以:

parent.addMouseListener(new MyLocalParentMouselistener(listenerForParent));

and implement an inner class (here: MyLocalParentMouselistener) that implements MouseListener and takes a MouseListener as Argument (and keep a reference to it). 并实现一个内部类(此处为:MyLocalParentMouselistener),该类实现MouseListener并将MouseListener作为参数(并保留对其的引用)。 Inside, you can do this: 在内部,您可以执行以下操作:

mouseClicked( MouseEvent e ){

    // Do some stuff here

    // assume we kept a reference to the passid-in mouseListener from 3rd Party class
    // in a class variable called "passedInMouseListenerInstance"
    passedInMouseListenerInstance.mouseClicked(e);
}

You could now do the same for the Child and pass both listeners and call them both if this is what you expect and want. 现在,您可以对Child进行相同的操作,并通过两个侦听器,并在您期望和期望的情况下将它们都调用。

Hope it is clearer now. 希望现在更加清楚。

Here is a tutorial on writing MouseListeners: http://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html 这是有关编写M​​ouseListener的教程: http : //docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM