简体   繁体   English

MouseMotionListener不响应JButton

[英]MouseMotionListener doesn't respond over JButtons

So I have a JPanel called contentPanel that holds 2 inner containers. 因此,我有一个名为contentPanel的JPanel,其中包含2个内部容器。 The first is a scorePanel that holds a few buttons, a slider and a label. 第一个是一个scorePanel ,其中包含一些按钮,一个滑块和一个标签。 The other is a buttonPanel which is a grid of JButtons. 另一个是buttonPanel ,它是JButton的网格。

在此处输入图片说明

In my contentPanel class, I have implemented the MouseMotionListener interface and added this listener to buttonPanel called buttons. 在我的contentPanel类中,我实现了MouseMotionListener接口,并将此侦听器添加到名为button的buttonPanel

The issue I'm having is that the mouseMoved method never gets called and I cannot get mouse coordinates while the mouse is hovering over a button. 我遇到的问题是,当鼠标悬停在按钮上方时,永远不会调用mouseMoved方法,并且无法获取鼠标坐标。 If instead I add the listener to each button, I get the mouse coordinates, but only as they relate to the origin of the button it's hovering inside of. 相反,如果我将侦听器添加到每个按钮,则可以获取鼠标坐标,但前提是它们与鼠标悬停在按钮的原点有关。 Additionally if I add the listener to the contentPanel , I get the mouse coordinates starting from the origin of that container, but it does not trigger the event over the buttons. 此外,如果将侦听器添加到contentPanel ,则将从该容器的原点开始获得鼠标坐标,但它不会触发按钮上的事件。

Can anyone explain how to mitigate this problem and get the mouse coordinates from the origin of the button panel without JButton obstruction? 谁能解释如何减轻此问题并从按钮面板的原点获取鼠标坐标而不会造成JButton障碍?

Tia. 蒂娅

*UPDATE: * I have made one change that has enabled the correct behavior I'm seeking, however only with a caveat. *更新:*我进行了一项更改,启用了我要寻找的正确行为,但是仅作了警告。 I adjusted the padding space between the buttons in the GridLayout to 15px and now when the mouse enters those in-between regions, the mouseEvent triggers. 我将GridLayout的按钮之间的填充空间调整为15px,现在当鼠标进入这些区域之间时, mouseEvent会触发。 This will enable the same effect as what I seek. 这将实现与我寻求的效果相同的效果。

I located some research about JButton's or other components consuming MouseEvents and not passing the event onto their parents. 我找到了一些有关JButton或其他使用MouseEvent且未将事件传递给其父项的组件的研究。 The solution was recommended by a member of the team at Sun/Oracle, which is to re-dispatch the event to the parent of the JComponent, which would be its container. Sun / Oracle团队的成员推荐了该解决方案,该解决方案是将事件重新分发给JComponent的父级,后者是其容器。

This was achieved with the following code: 这是通过以下代码实现的:

JComponent subComponent = new JButton("JButton");    
MouseAdapter redispatcher = new MouseAdapter()
{
  @Override
  public void mouseEntered(MouseEvent evt)
  {
    dispatchMouseEvent(evt);
  }      
  @Override
  public void mouseExited(MouseEvent evt)
  {
    dispatchMouseEvent(evt);
  }
  @Override
  public void mouseMoved(MouseEvent evt)
  {
    dispatchMouseEvent(evt);
  }
  @Override
  public void mousePressed(MouseEvent evt)
  {
    dispatchMouseEvent(evt);
  }
  private void dispatchMouseEvent(MouseEvent evt)
  {
    Container parent = evt.getComponent().getParent();
    parent.dispatchEvent(SwingUtilities.convertMouseEvent(evt.getComponent(), evt, parent));
  }            
};
subComponent.addMouseListener(redispatcher);
subComponent.addMouseMotionListener(redispatcher);

Which was inevitably a great solution to passing events along. 这不可避免地是传递事件的好方法。 ( http://www.jyloo.com/news/?pubId=1315817317000 ) http://www.jyloo.com/news/?pubId=1315817317000

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

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