2

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

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

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

    }
});

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

4

1 回答 1

4

您必须手动完成,但您需要 aMouseAdapter而不是 aMouseMotionAdapter来记录初始 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;
    }
});
于 2013-09-03T20:51:20.967 回答