如何知道mouseDragged方向 - 何时向上或向下?

时间:2013-09-03 20:37:38

标签: java swing mouseevent

完成创建自定义滚动条,问题是:

addMouseMotionListener(new MouseMotionAdapter() {
    @Override
    public void mouseDragged(MouseEvent e) {

        //How to know mouse direction - up or down?

    }
});

也许有一些简单的方法,或者我必须手动完成?

1 个答案:

答案 0 :(得分:4)

您必须手动执行此操作,但您需要MouseAdapter而不是MouseMotionAdapter来记录初始Y坐标。

addMouseMotionListener(new MouseAdapter() {

    int previousY;

    @Override
    public void mousePressed(MouseEvent e) {
        previousY = e.getY();
    }

    @Override
    public void mouseDragged(MouseEvent e) {

        int y = e.getY();
        if (y < previousY) {
            System.out.println("UP");
        } else if (y > previousY) {
            System.out.println("DOWN");
        }

        previousY = y;
    }
});