java - MouseAdapter methods - why `mouseDragged`, `mouseMoved` and `mouseWheelMoved` included? -
my goal simplify code (all works fine):
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 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 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 being used, working fine } @override public void mousemoved(mouseevent e) { } }
but if decide use mouseadapter
this:
addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { transferfocusbackward(); //this method being used, working fine } @override public void mousedragged(mouseevent e) { //this method being used, not working anymore } });
mousedragged
receiving no events. if add mousemotionlistener
this:
addmouselistener(new mouseadapter() { @override public void mousepressed(mouseevent e) { //this method being used, working fine } }); addmousemotionlistener(new mousemotionadapter() { @override public void mousedragged(mouseevent e) { //this method being used, working fine } });
then backs normal. question - purpose of mousedragged
, mousemoved
, mousewheelmoved
methods in mouseadapter
class if not work?
ok, think understand now: in mouseadapter
class documentation said "create listener object using extended class , register component using component's addmouselistener
, addmousemotionlistener
, addmousewheellistener
methods". looks like:
private class mouselisteners extends mouseadapter { @override public void mousepressed(mouseevent e) { //this method being used, working fine } @override public void mousedragged(mouseevent e) { //this method being used, working fine } }
and then:
mouselisteners listeners = new mouselisteners(); addmouselistener(listeners); addmousemotionlistener(listeners);
now ok.
Comments
Post a Comment