1. 通过方法调用
Button push = new Button("Push Me!");
push.setOnAction(this::processButtonEven);
“:”操作符指定方法引用,它是在Java8中新增加的。
方法指向类中的processButtonEven,this指的是当前正在执行方法的对象
private void processButtonEven(ActionEvent event) {
//事件事件要执行的代码
}
2. 通过内部私有类
Button push = new Button("Push Me!");
push.setOnAction(new ButtonHandler());
private class ButtonHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
//事件事件要执行的代码
}
}
3. lambda表达式定义方法
Button push = new Button("Push Me!");
push.setOnAction((event)->{
//事件事件要执行的代码
});
其中this::processButtonPress等价于event->processButtonPress(event)