如何注册以获得有关侦听器对象事件的通知?
基本上我想要实现的目标可以通过以下方式实现:
this.mouseMoved()= listener.mouseMoved()
Class Secondary implements MouseMotionListener {
public Secondary(MouseMotionListener listener) {
// Here Secondary needs to be registered to the same observer that
// listener is getting notified from so that the Secondary class's
// mouseMoved() and mouseDragged() both fire at the same time and
// with the same MouseEvents as the listener's.
}
@Override
void mouseMoved(MouseEvent e) {
// This needs to be essentially the same as the events fired
// in the constructor's parameter object
}
@Override
void mouseDragged(MouseEvent e) {
// This needs to be essentially the same as the events fired
// in the constructor's parameter object
}
}
答案 0 :(得分:0)
监听器被调用方法的人触发,JComponents接受mouselisteners作为参数,因此你必须设置它:
JPanel panel = new JPanel();
jFrame.add(panel);
panel.addMouseListener(new Secondary());
如果要调度事件,可以使用委托模式。
class Secondary implements MouseMotionListener {
MouseMotionListener other;
public Secondary(MouseMotionListener listener) {
other = listener;
}
@Override
void mouseMoved(MouseEvent e) {
other.mouseMoved(e);
}
@Override
void mouseDragged(MouseEvent e) {
other.mouseDragged(e);
}
}