gpt4 book ai didi

java - 如何更改多个按钮的MouseReleaseEvent?

转载 作者:太空宇宙 更新时间:2023-11-04 12:10:39 25 4
gpt4 key购买 nike

我试图弄清楚如何一次更改多个按钮的 OnMouseReleaseEvent。该应用程序是一个屏幕钢琴,我想删除释放事件,以便弹奏的音符能够持续,而不是在短按一下后关闭。

我的代码片段适用于一个键:

@FXML
public void changeOnMouseRelease(){
if (getOriginalEvent() == null) {
setOriginalEvent(keyC.getOnMouseReleased());
}
if (getOriginalEvent() == keyC.getOnMouseReleased()) {
keyC.setOnMouseReleased(event -> {
System.out.println("Mouserelease-Event empty \n");
});
} else keyC.setOnMouseReleased(getOriginalEvent());
}
}

这些是原始事件的 getter 和 setter:

private void setOriginalEvent(EventHandler originalEvent){
this.originalEvent = originalEvent;
}

private EventHandler getOriginalEvent(){
return this.originalEvent;
}

当我尝试这个时,它不起作用:

@FXML
public void changeOnMouseRelease(){
for(Button b : pianoKeys) { // pianoKeys is just an array of the 13 piano buttons
if (getOriginalEvent() == null) {
setOriginalEvent(b.getOnMouseReleased());
}
if (getOriginalEvent() == b.getOnMouseReleased()) {
b.setOnMouseReleased(event -> {
System.out.println("Mouserelease-Event empty \n");
});
} else b.setOnMouseReleased(getOriginalEvent());
}
}

我假设它与 getter/setter 有关,但我不确定如何正确处理这个问题。

最佳答案

通过向事件处理程序添加 MouseEvent 参数,您可以使用此信息来检索按下的 Button。您可以将允许您选择如何处理按键的信息与 Button 关联起来,例如使用用户数据:

Controller

@FXML
private void onMouseReleased(MouseEvent event){
Button b = (Button) event.getSource();
System.out.println("released key "+b.getUserData());
}


@FXML
private void onMousePressed(MouseEvent event){
Button b = (Button) event.getSource();
System.out.println("pressed key "+b.getUserData());
}

fxml

...
<Button onMousePressed="#onMousePressed" onMouseReleased="#onMouseReleased" userData="D#" />
...
<小时/>

这消除了循环遍历按钮的需要。

关于java - 如何更改多个按钮的MouseReleaseEvent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39894107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com