我在控制器类中有以下这段代码:
public void chooseFromBox() {
for (Node n : myHbox.getChildren()) {
ScaleTransition transition = new ScaleTransition(Duration.seconds(0.3), n);
transition.setByX(0.7);
transition.setByY(0.7);
transition.setAutoReverse(true);
transition.setCycleCount(2);
transition.play();
n.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
transition.stop();
int i = myHbox.getChildren().indexOf(event.getSource());
for (Node n : myHbox.getChildren()) {
n.removeEventHandler(MouseEvent.MOUSE_CLICKED, this);
// This instruction doesn't seem to work
}
gui.getNetworkHandler().doSomething(i);
}
});
}
}
我需要在单击其中一个孩子后立即将EventHandler移走。使用n.removeHandler(MouseEvent.MOUSE_CLICKED, this);
的行被执行(我可以从调试中看到它),但是在执行selectFromBox()方法之后,我可以再次单击其中一个子级,它将再次触发。
编辑:遵循@Itai建议后,我将代码更改为此:
public void chooseFromBox() {
for (Node n : myHbox.getChildren()) {
ScaleTransition transition = new ScaleTransition(Duration.seconds(0.3), n);
transition.setByX(0.7);
transition.setByY(0.7);
transition.setAutoReverse(true);
transition.setCycleCount(2);
transition.play();
EventHandler<MouseEvent> handler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
transition.stop();
int i = myHbox.getChildren().indexOf(event.getSource());
n.removeEventHandler(MouseEvent.MOUSE_CLICKED, this);
gui.getNetworkHandler().doSomething(i);
}
};
n.setOnMouseClicked(handler);
}
}
我有另一种相似的方法,但是相同的方法不起作用:
void useThing(ActionEvent event) {
for (Node n : otherHbox.getChildren()) {
EventHandler<MouseEvent> handler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
int i = otherHbox.getChildren().indexOf(event.getSource());
for (Node n : otherHbox.getChildren()) {
n.removeEventHandler(MouseEvent.MOUSE_CLICKED, this);
}
gui.getNetworkHandler().doSomethingElse(i);
}
};
n.setOnMouseClicked(handler);
}
}
我想念什么?
答案 0 :(得分:0)
也许尝试换掉这个
EventHandler<MouseEvent> handler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
transition.stop();
int i = myHbox.getChildren().indexOf(event.getSource());
n.removeEventHandler(MouseEvent.MOUSE_CLICKED, this);
gui.getNetworkHandler().doSomething(i);
}
};
n.setOnMouseClicked(handler);
有了这个
n.setOnMouseClicked(event -> {
transition.stop();
int i = myHbox.getChildren().indexOf(event.getSource());
//n.removeEventHandler(MouseEvent.MOUSE_CLICKED, this);
n.setOnMouseClicked(Event::consume);
gui.getNetworkHandler().doSomething(i);
});
类似地交换
EventHandler<MouseEvent> handler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
int i = otherHbox.getChildren().indexOf(event.getSource());
for (Node n : otherHbox.getChildren()) {
n.removeEventHandler(MouseEvent.MOUSE_CLICKED, this);
}
gui.getNetworkHandler().doSomethingElse(i);
}
};
n.setOnMouseClicked(handler);
与此
n.setOnMouseClicked(event -> {
int i = otherHbox.getChildren().indexOf(event.getSource());
for (Node node : otherHbox.getChildren()) {
node.setOnMouseClicked(Event::consume);
}
gui.getNetworkHandler().doSomethingElse(i);
});
我在测试中使用了消耗来删除可能要根据其他操作更改的事件