添加MouseListener会中断鼠标事件调度

时间:2016-02-23 15:31:01

标签: java swing drag-and-drop mouseevent

我目前正在尝试修复通过向UPDATE MyTable SET column = stuff(column,patindex('%[0-9]%',column),0,'9') 添加任意java.awt.event.MouseListener而引入的错误。以下代码是组件的拖放实现的简化版本:

  1. 当按下某个组件时,它会从其父组件中删除,并可以在第二个窗口中在屏幕上拖动
  2. 当用户释放鼠标时,该组件会添加到其他组件中(为简单起见,它会在提供的示例中再次添加到其原始父组件中)
  3. 以下是代码:

    java.awt.Component

    现在出于某种原因,如果向拖动的标签添加任意类型的import java.awt.AWTEvent; import java.awt.Color; import java.awt.Toolkit; import java.awt.event.AWTEventListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JWindow; import javax.swing.SwingUtilities; public class DragComponentBug { public static void main(String[] args) { // set to true to enable the broken behavior boolean addMouseListener = false; // set to false to fix the broken behavior by not removing the label from its parent boolean removeLabel = true; SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocation(100, 100); JWindow dragWindow = new JWindow(); if (!removeLabel) { JPanel panel = new JPanel(); panel.setBorder(BorderFactory.createLineBorder(Color.black)); dragWindow.add(panel); } JLabel label = new JLabel("Drag me around"); frame.add(label); MouseAdapter labelListener = new MouseAdapter(){ @Override public void mousePressed(MouseEvent e) { System.err.println("mouse pressed: " + e); } @Override public void mouseReleased(MouseEvent e) { System.err.println("mouse released: " + e); } @Override public void mouseDragged(MouseEvent e) { System.err.println("mouse dragged: " + e); } }; if (addMouseListener) { label.addMouseListener(labelListener); label.addMouseMotionListener(labelListener); } Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { @Override public void eventDispatched(AWTEvent event) { MouseEvent e = (MouseEvent)event; switch (e.getID()) { case MouseEvent.MOUSE_PRESSED: // System.out.println("mouse pressed:" + event); if (removeLabel) { label.setBorder(BorderFactory.createLineBorder(Color.black)); dragWindow.add(label); } dragWindow.setLocation(e.getLocationOnScreen()); dragWindow.setSize(label.getSize()); dragWindow.setVisible(true); frame.repaint(); break; case MouseEvent.MOUSE_RELEASED: // System.out.println("mouse released:" + event); if (removeLabel) { label.setBorder(null); frame.add(label); } frame.repaint(); dragWindow.setVisible(false); break; case MouseEvent.MOUSE_DRAGGED: // System.out.println("mouse dragged:" + event); dragWindow.setLocation(e.getLocationOnScreen()); break; default: break; } } }, AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK); frame.pack(); frame.setVisible(true); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { dragWindow.dispose(); } }); } }); } } (或MouseListener),那么拖放行为就会被破坏,全局{{1} }没有接收任何拖动或释放鼠标事件(添加的侦听器也没有)。这可以通过在提供的示例中将MouseMotionListener设置为AWTEventListener来进行测试。

    这里的另一个重要方面是,这种行为在某种程度上似乎与事实有关,即在按下鼠标后标签会从其父项中删除。如果没有删除标签,则事件调度将继续有效。在addMouseListener设置为true后,可以通过将removeLabel设置为false来测试此问题。

    为什么在将侦听器添加到标签后不再调度鼠标事件,这与从父项中删除标签有何关系?即使将监听器添加到标签中(包括在拖动操作期间删除/添加标签),有没有办法恢复预期的行为?

    感谢您的帮助

0 个答案:

没有答案