我的目标是简化这段代码(一切正常):
package test;
import java.awt.Window;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JWindow;
public class A extends JWindow implements MouseListener, MouseMotionListener {
public A() {
addMouseListener(A.this);
addMouseMotionListener(A.this);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
//This method is being used, working fine
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
//This method is being used, working fine
}
@Override
public void mouseMoved(MouseEvent e) {
}
}
但如果我决定这样使用MouseAdapter
:
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
transferFocusBackward();
//This method is being used, working fine
}
@Override
public void mouseDragged(MouseEvent e) {
//This method is being used, not working anymore
}
});
mouseDragged
不接收任何事件。但如果我MouseMotionListener
这样添加:
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
//This method is being used, working fine
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
//This method is being used, working fine
}
});
然后一切恢复正常。所以我的问题是 -如果它们不起作用,课堂mouseDragged
上的目的mouseMoved
和mouseWheelMoved
方法是什么?MouseAdapter