java.awt.event.MouseAdapter java.awt.event.KeyAdapter java.awt.event.WindowAdapter 等等都是适配器模式
// 运行适配器的例子
public class TwoListennerAdapter {
private Frame frame;
private TextField textField;
public static void main(String[] args) {
TwoListen twoListen = new TwoListen();
twoListen.go();
}
public void go(){
frame = new Frame("two listenners example");
frame.add(new Label("click"),BorderLayout.NORTH);
textField = new TextField();
frame.add(textField,BorderLayout.SOUTH);
// 增加一个鼠标的监听
frame.addMouseListener(new MyAdapter());
frame.setSize(300,200);
frame.setVisible(true);
}
}
// 继承这个适配器后,就不需要实现鼠标所有的方法,由适配器帮你实现了
class MyAdapter extends MouseAdapter{
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("--enter--");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("--exit--");
}
}
// jdk的源码MouseAdapter,原理就是做了很多空的实现
public abstract class MouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener {
/**
* {@inheritDoc}
*/
public void mouseClicked(MouseEvent e) {}
/**
* {@inheritDoc}
*/
public void mousePressed(MouseEvent e) {}
/**
* {@inheritDoc}
*/
public void mouseReleased(MouseEvent e) {}
/**
* {@inheritDoc}
*/
public void mouseEntered(MouseEvent e) {}
/**
* {@inheritDoc}
*/
public void mouseExited(MouseEvent e) {}
/**
* {@inheritDoc}
* @since 1.6
*/
public void mouseWheelMoved(MouseWheelEvent e){}
/**
* {@inheritDoc}
* @since 1.6
*/
public void mouseDragged(MouseEvent e){}
/**
* {@inheritDoc}
* @since 1.6
*/
public void mouseMoved(MouseEvent e){}
}