完成创建自定义滚动条后,问题是:
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
//How to know mouse direction - up or down?
}
});
也许有一些简单的方法,还是我必须手动进行?
最佳答案
您必须手动进行操作,但是需要一个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;
}
});