gpt4 book ai didi

JavaFx-如果值全部相同,如何获取按钮数组索引

转载 作者:行者123 更新时间:2023-12-02 10:36:59 24 4
gpt4 key购买 nike

当我尝试创建具有相同“x”值的多个按钮时遇到问题。如何解决此问题,或者获取按钮的值,然后删除与按钮具有相同索引的数组中的特定元素,而不在循环 for 循环时删除其他元素?

Button[] delButton = new Button[sizeOfIt]; //sizeOfIt is the size of array

for (int m=0; m <sizeOfIt; m++) {
delButton[m] = new Button("x");
}


for(int x = 0; x < delButton.length; x++) {
delButton[x].setOnAction(new EventHandler<ActionEvent>() {

public void handle(ActionEvent event) {
// delete the element in the array with the same index as my button i clicked
}
});
}

最佳答案

您可以使用按钮的位置来处理此问题,它会在按钮所在的位置留下一个空框:

for(int x = 0; x < delButton.length; x++) {     
final index = x;
delButton[x].setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
delButton[index] = null;
}
});
}

关于JavaFx-如果值全部相同,如何获取按钮数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53214202/

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