简体   繁体   English

在Swing中未按下鼠标的组件上侦听鼠标释放事件

[英]Listen for mouse released event on component on which the mouse was not pressed in Swing

Is it possible to listen for mouse released event on the component on which it was not pressed? 是否可以在未按下组件的组件上监听鼠标释放事件?

I know that when mouse is released MouseListener.mouseReleased() is invoked on the listeners for that component when mouse press originated even if the cursor is above other component. 我知道释放鼠标后,即使光标在其他组件上方,也可以在发起鼠标按下时在该组件的侦听器上调用MouseListener.mouseReleased()

How to inform a component or its listeners that the mouse was over it and it was released? 如何通知组件或其侦听器鼠标悬停在组件上方并释放它?

If you add your MouseListener to the container that holds your components of interest, you can find out which component the mouse is over on press or drag. 如果将MouseListener添加到保存您感兴趣的组件的容器中,则可以找出在按下或拖动时鼠标悬停在哪个组件上。 For instance in the code below, I've added my MouseAdapter (a combination MouseListener, MouseMotionListener, and MouseWheelListener) to the containing JPanel, and after getting the location of the mouse event on the container, I call getComponentAt(Point p) on my container to get the child component that the mouse was over: 例如,在下面的代码中,我已将MouseAdapter(MouseListener,MouseMotionListener和MouseWheelListener的组合)添加到包含的JPanel中,并在容器上获得了鼠标事件的位置之后,在我的容器上调用了getComponentAt(Point p)容器以获取鼠标移过的子组件:

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class TestMouseRelease extends JPanel {
   private String[] panelNames = { "Panel A", "Panel B" };

   public TestMouseRelease() {
      setLayout(new GridLayout(1, 0));
      MouseAdapter mAdapter = new MyMouseAdapter();

      addMouseListener(mAdapter);
      addMouseMotionListener(mAdapter);

      for (String panelName : panelNames) {
         JPanel panel = new JPanel();
         panel.setName(panelName);
         // panel.addMouseListener(mAdapter);
         // panel.addMouseMotionListener(mAdapter);
         panel.setBorder(BorderFactory.createTitledBorder(panelName));
         panel.add(Box.createRigidArea(new Dimension(300, 300)));
         add(panel);
      }
   }

   private class MyMouseAdapter extends MouseAdapter {
      @Override
      public void mousePressed(MouseEvent e) {
         displayInfo(e, "mousePressed");
      }

      @Override
      public void mouseReleased(MouseEvent e) {
         displayInfo(e, "mouseReleased");
      }

      @Override
      public void mouseDragged(MouseEvent e) {
         displayInfo(e, "mouseDragged");
      }

      private void displayInfo(MouseEvent e, String info) {
         JComponent comp = (JComponent) e.getSource();
         Component childComp = comp.getComponentAt(e.getPoint());
         if (childComp != null) {
            String name = childComp.getName();
            System.out.println(name + ": " + info);
         }
      }
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("TestMouseRelease");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new TestMouseRelease());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

Yeah, I handled something similar in my project. 是的,我在项目中处理了类似的事情。 I use getBounds() on all component in the container and check if they contain the x,y coordinates of your mouse. 我在容器中的所有组件上使用getBounds() ,并检查它们是否包含鼠标的x,y坐标。 Below is my code: 下面是我的代码:

Component[] components = jPanel1.getComponents();
for (Component c : components) {

    if (c.getBounds().contains(ev.getXOnScreen(), ev.getYOnScreen())) {
        System.out.println(c.getClass());
        y = ch.getPosY();
        x = ch.getPosX();
    }
}

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

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