我试图在java中同时使用mouseClicked和mouseDragged。但mouseClicked属于MouseListener,mouseDragged属于MouseMotionListener。任何帮助将不胜感激,谢谢。
答案 0 :(得分:1)
为了方便起见,您可以查看MouseAdapter
,其中MouseListener
,MouseMotionListener
和MouseWheelListener
MouseAdapter ma = new MouseAdapter() {
public void mouseClicked(MosueEvent evt) {
// Do what ever here
}
public void mouseDragged(MosueEvent evt) {
// Do what ever here
}
}
yourComponent.addMouseListener(ma);
yourComponent.addMouseMotionListener(ma);
有关详细信息,请参阅How to Write a Mouse Listener
同样,你可以创建自己的类......
public class MyAwesomeMouseHandler implements MouseListener, MouseMotionListener {
// Implement the required methods...
}
然后你只需创建一个实例添加注册它们......
MyAwesomeMouseHandler handler = new MyAwesomeMouseHandler();
yourComponent.addMouseListener(handler);
yourComponent.addMouseMotionListener(handler);
你可以分别实现侦听器并分别注册它们......