如何在java中使用事件侦听器切换对象?
我需要你的帮助。我正在制作一个使用Wiimote的控制程序,我需要制作两种不同类型的控制。每个控制器代码都在controlType1和controlType2类中定义(此处不包括#2,但主要与#1相同) 想法是,当我按下WiiMote上的某个按钮时,控制器从1型切换到2型。我已经实例化了2个对象,当按下按钮时,它应该删除其中一个对象的侦听器,并将其更改为另一个对象 目前,我已经走了这么远,被困在这里。你知道我该怎么做吗如何在java中使用事件侦听器切换对象?,java,events,object,listener,Java,Events,Object,Listener,我需要你的帮助。我正在制作一个使用Wiimote的控制程序,我需要制作两种不同类型的控制。每个控制器代码都在controlType1和controlType2类中定义(此处不包括#2,但主要与#1相同) 想法是,当我按下WiiMote上的某个按钮时,控制器从1型切换到2型。我已经实例化了2个对象,当按下按钮时,它应该删除其中一个对象的侦听器,并将其更改为另一个对象 目前,我已经走了这么远,被困在这里。你知道我该怎么做吗 public class WiiDroneControl implement
public class WiiDroneControl implements ControlSwitchListener {
private Wiimote wiimote;
private WiimoteListener control1 = (WiimoteListener) new controlType1(this);
private WiimoteListener control2 = (WiimoteListener) new controlType2(this);
public WiiDroneControl() {
Wiimote wiimotes[] = WiiUseApiManager.getWiimotes(1, true);
if(wiimotes!= null && wiimotes.length > 0)
{
wiimote = wiimotes[0];
wiimote.addWiiMoteEventListeners(control1);
wiimote.addWiiMoteEventListeners(control2);
wiimote.activateMotionSensing();
wiimote.activateContinuous();
wiimote.getStatus();
}
}
@Override
public void onSwitchEvent() {
// TODO Auto-generated method stub
}
}
另一类
public class controlType1 implements WiimoteListener{
ControlSwitchListener listener = null;
public controlType1(ControlSwitchListener l) {
listener = l;
}
@Override
public void onButtonsEvent(WiimoteButtonsEvent e) {
// TODO Auto-generated method stub
listener.onSwitchEvent();
if (e.isButtonOnePressed())
{
//switch controller object when this button is pressed
}
}
}
如果我理解你的问题
public class WiiDroneControl implements ControlSwitchListener {
private Wiimote wiimote;
private WiimoteListener control1 = new controlType1(this);
private WiimoteListener control2 = new controlType2(this);
private WiimoteListener current = control1;
public WiiDroneControl() {
Wiimote wiimotes[] = WiiUseApiManager.getWiimotes(1, true);
if(wiimotes!= null && wiimotes.length > 0)
{
wiimote = wiimotes[0];
wiimote.addWiiMoteEventListeners(current);
wiimote.activateMotionSensing();
wiimote.activateContinuous();
wiimote.getStatus();
}
}
@Override
public void onSwitchEvent() {
current = current.equals(control1) ? control2 : control1;
}
}
“Wiimote”那是什么?从没听说过任天堂Wii?那是控制器…从没听说过链接到信息源?如果没有标记,这表明它是必要的。您可能需要阅读更多关于[如何在Java中抛出和捕获事件][1]的信息。[1] :如何从类controlType1传递事件,以便其他类中的onSwitchEvent()方法能够识别?无论如何,谢谢你的帮助。请你能更清楚地描述一下你的任务吗。您有两个控制器,需要识别哪个控制器被触发?