Java MouseStener屏蔽另一个MouseStener

Java MouseStener屏蔽另一个MouseStener,java,swing,listener,jscrollpane,mouselistener,Java,Swing,Listener,Jscrollpane,Mouselistener,我制作了一个DragScrollPane,它扩展了JScrollPane。您可以在窗格中单击并拖动组件,而不是使用滚动条。如果在此DragSP中向该组件添加鼠标侦听器,则将无法再拖动窗格。我的理论是DragSP中组件上的侦听器屏蔽了DragSP中的侦听器。这是我的密码: DragScrollPane.java package gui.game.map; import java.awt.Cursor; import java.awt.Point; import java.awt.Rectangl

我制作了一个
DragScrollPane
,它扩展了
JScrollPane
。您可以在窗格中单击并拖动组件,而不是使用滚动条。如果在此
DragSP
中向该组件添加鼠标侦听器,则将无法再拖动窗格。我的理论是
DragSP
中组件上的侦听器屏蔽了
DragSP
中的侦听器。这是我的密码:

DragScrollPane.java

package gui.game.map;

import java.awt.Cursor;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JViewport;

public class DragScrollPane extends JScrollPane {
    private static final long serialVersionUID = 1L;

    public DragScrollPane(JComponent objectToMove, boolean autoScroll) {
        super(objectToMove);
        ViewportDragScrollListener l = new ViewportDragScrollListener(
                objectToMove, autoScroll);
        JViewport gridScrollPaneViewport = getViewport();
        gridScrollPaneViewport.addMouseMotionListener(l);
        gridScrollPaneViewport.addMouseListener(l);
        gridScrollPaneViewport.addHierarchyListener(l);
    }

    class ViewportDragScrollListener extends MouseAdapter implements
            HierarchyListener {
        private static final int SPEED = 4;
        private static final int DELAY = 10;
        private final Cursor dc;
        private final Cursor hc = Cursor
                .getPredefinedCursor(Cursor.HAND_CURSOR);
        private final javax.swing.Timer scroller;
        private final JComponent label;
        private final Point startPt = new Point();
        private final Point move = new Point();
        private boolean autoScroll = false;

        public ViewportDragScrollListener(JComponent comp, boolean autoScroll) {
            this.label = comp;
            this.autoScroll = autoScroll;
            this.dc = comp.getCursor();
            this.scroller = new javax.swing.Timer(DELAY, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    JViewport vport = (JViewport) label.getParent();
                    Point vp = vport.getViewPosition();
                    vp.translate(move.x, move.y);
                    label.scrollRectToVisible(new Rectangle(vp, vport.getSize()));
                }
            });
        }

        public void hierarchyChanged(HierarchyEvent e) {
            JComponent c = (JComponent) e.getSource();
            if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0
                    && !c.isDisplayable() && autoScroll) {
                scroller.stop();
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            JViewport vport = (JViewport) e.getSource();
            Point pt = e.getPoint();
            int dx = startPt.x - pt.x;
            int dy = startPt.y - pt.y;
            Point vp = vport.getViewPosition();
            vp.translate(dx, dy);
            label.scrollRectToVisible(new Rectangle(vp, vport.getSize()));
            move.setLocation(SPEED * dx, SPEED * dy);
            startPt.setLocation(pt);
        }

        @Override
        public void mousePressed(MouseEvent e) {
            ((JComponent) e.getSource()).setCursor(hc);
            startPt.setLocation(e.getPoint());
            move.setLocation(0, 0);
            if (autoScroll) {
                scroller.stop();
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            ((JComponent) e.getSource()).setCursor(dc);
            if (autoScroll) {
                scroller.start();
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            ((JComponent) e.getSource()).setCursor(dc);
            move.setLocation(0, 0);
            if (autoScroll) {
                scroller.stop();
            }
        }
    }
}

是的,这是正确的,如果z顺序较高的组件有一个mouselister,鼠标事件将不会传播到它下面的组件,这就是它的工作方式。是否考虑将鼠标侦听器添加到VIEW端口视图中?查看端口视图是什么?添加到视口中的组件,即窃取鼠标事件;代码>“什么是视图端口视图?”——这是JScrollPane保存和滚动的组件。它实际上是JScrollPane的视口视图,我在ScrollPane中有一个JPanel,里面有多个组件。我希望能够拖动整个东西,并告诉用户在里面的每一个点击。