我打算创建一个EventHandler类,它将处理JavaFX类中所有控件的所有类型的事件。
例如,我添加了我的自定义事件处理程序类,以下面的方式处理Action事件,它只是工作正常:
userNameText.addEventHandler(ActionEvent.ACTION, new DataChangeHandler());
cmbBox.addEventHandler(ActionEvent.ACTION, new DataChangeHandler());
btn.addEventHandler(ActionEvent.ACTION, new DataChangeHandler());
这是我的custome事件处理程序类代码:
public class DataChangeHandler implements EventHandler<ActionEvent> {
public void handle(ActionEvent event) {
System.out.println("My Very Own Private Button Handler");
}
}
但是当我尝试将其中一个addEventHandlers更改为MouseEvent并以下面的方式修改主EventHandler类时,它会显示错误“接口事件处理程序不能使用不同的参数多次实现”:
userNameText.addEventHandler(ActionEvent.ACTION, new DataChangeHandler());
cmbBox.addEventHandler(MouseEvent.CLICKED, new DataChangeHandler());
btn.addEventHandler(ActionEvent.ACTION, new DataChangeHandler());
public class DataChangeHandler implements EventHandler<ActionEvent>, EventHandler<MouseEvent> {
public void handle(ActionEvent event) {
System.out.println("My Very Own Private Button Handler - ACTIONEVENT");
}
@Override
public void handle(MouseEvent arg0) {
System.out.println("My Very Own Private Button Handler - MOUSEEVENT");
}
}
还有其他方法可以达到这个目的吗?请帮忙。提前谢谢。
答案 0 :(得分:6)
试试这个
public class DataChangeHandler implements EventHandler<Event>{
@Override
public void handle(Event event) {
System.out.println("My Very Own Private Handler For All Kind Of Events");
}
}