Java JScrollPane 通过拖动来移动

标签 java swing jpanel jscrollpane drag

如果我创建一个 JScrollPane 并在其中添加一个 JPanel,我希望能够通过在 JScollPane 内单击拖动来滚动 Pane 。

最佳答案

我制作了一个自定义 JScrollPane,像使用普通 JScrollPane 一样使用它。

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) {
        super(objectToMove);
        ViewportDragScrollListener l = new ViewportDragScrollListener(
                objectToMove, false);
        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();
            }
        }
    }
}

关于Java JScrollPane 通过拖动来移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22873405/

相关文章:

java - 在当前鼠标位置检测图像仅适用于部分图像

java - 如何将 JPanel 中的组件左对齐?

Java:如何等待 updateUI?

java - 数据完整性违规异常 : Unable to store JSON Schema with spring mongodb

java - 提供抽象方法的实现但限制可见性

Java Swing - 在 JPanel 中获取源代码

java - JList 不会出现在 JFrame 上

java - 大字节数组传输到客户端

java - 将 PrinterWriter 与字符串 ArrayList 结合使用

java - 在clearRect()和repaint()之后无法在JPanel上绘画