在 javaFX 中使用 arrayList 实现大量按钮
Implementing numerous buttons using an arrayList in javaFX
我正在为餐厅菜单实现一个用户界面,它涉及大量递增和递减按钮,如下所示 - 请注意我使用 SceneBuilder 创建了 UI:
我已经尝试了很多方法来允许循环遍历每个按钮并在单击鼠标时更新它旁边的文本字段但是未能通过 arrayList 实现这一点 - 当我知道 arraylist 是成功填满按钮。
这是我尝试过的:
我有一个方法可以通过指定相应按钮的 fxid 来填充列表 -
plusButtonList.add(soupPlus);
plusButtonList.add(guacPlus);
plusButtonList.add(empanadaPlus);
plusButtonList.add(jalapenoPlus);
plusButtonList.add(quesaPlus);
然后我还有一个方法应该遍历每个按钮,设置 onaction 以从文本字段检索当前计数器值(它也在列表中,但相应的按钮和文本字段将是在相同的索引位置)此方法在控制器 class 的 'initialise' 中调用。
private void checkIfIncrement() {
for(int i = 0; i < 20; i++) {
int counter = i;
plusButtonList.get(i).setOnAction(e -> {
int currentCountVal = foodCounterList.get(counter);
theTextFields.get(counter).setText(Integer.toString(currentCountVal+ 1));
foodCounterList.set(counter, currentCountVal+1);
});
}
注意:foodCounterList 是包含每个文本字段当前值的整数列表。 'theTextFields' 是包含每个文本字段的列表。
更新:这是我的控制器 Class 在尝试实施 Matt 建议的改进后
package customer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
/**
* View for the customer.
* @author Arslan Nazir
*
*/
public class CustomerView {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private ImageView imageview;
Stage stage;
@FXML
private Button soupPlus;
@FXML
private Button soupMinus;
@FXML
private TextField soupField;
@FXML
private Button guacMinus;
@FXML
private Button guacPlus;
@FXML
private TextField guacField;
@FXML
private Font x11322;
@FXML
private Button empanadaMinus;
@FXML
private TextField empanadaField;
@FXML
private Button empanadaPlus;
@FXML
private Button jalapenoMinus;
@FXML
private Button jalapenoPlus;
@FXML
private TextField jalapenoField;
@FXML
private Button quesaMinus;
@FXML
private Button quesaPlus;
@FXML
private TextField quesaField;
@FXML
private Button choriMinus;
@FXML
private Button choriPlus;
@FXML
private TextField choriField;
@FXML
private Button seafoodMinus;
@FXML
private Button seafoodPlus;
@FXML
private TextField seafoodField;
@FXML
private Button avocadoMinus;
@FXML
private Button avocadoPlus;
@FXML
private TextField avocadoField;
@FXML
private Button burritoMinus;
@FXML
private Button burritoPlus;
@FXML
private TextField burritoField;
@FXML
private Button chilliMinus;
@FXML
private Button chilliPlus;
@FXML
private TextField chilliField;
@FXML
private Font x113221;
@FXML
private Button churrosMinus;
@FXML
private Button churrosPlus;
@FXML
private TextField churrosField;
@FXML
private Button pastelMinus;
@FXML
private Button pastelPlus;
@FXML
private TextField pastelField;
@FXML
private Button brownieMinus;
@FXML
private Button browniePlus;
@FXML
private TextField brownieField;
@FXML
private Button strawbMinus;
@FXML
private Button strawbPlus;
@FXML
private TextField strawbField;
@FXML
private Button vanillaMinus;
@FXML
private Button vanillaPlus;
@FXML
private TextField vanillaField;
@FXML
private Font x11321;
@FXML
private Button colaPlus;
@FXML
private Button colaMinus;
@FXML
private TextField colaField;
@FXML
private Button dietColaMinus;
@FXML
private Button dietColaPlus;
@FXML
private TextField dietColaField;
@FXML
private Button mojitoMinus;
@FXML
private Button mojitoPlus;
@FXML
private TextField mojitoField;
@FXML
private Button blueMargMinus;
@FXML
private Button blueMargPlus;
@FXML
private TextField blueMargField;
@FXML
private Button redMargMinus;
@FXML
private Button redMargPlus;
@FXML
private TextField redMargField;
@FXML
private Button bottleWaterMinus;
@FXML
private Button bottleWaterPlus;
@FXML
private TextField bottleWaterField;
@FXML
private Button tapWaterMinus;
@FXML
private Button tapWaterPlus;
@FXML
private TextField tapWaterField;
@FXML
private Button closeprogram;
@FXML
private Button minimiseprogram;
@FXML
private VBox bgone;
@FXML
private Button callwaiter;
@FXML
private TextField summaryField;
static List<TextField> theTextFields = new ArrayList<>();
ArrayList<Button> plusButtonList = new ArrayList<>(Arrays.asList(soupPlus, guacPlus));
static List<Button> minusButtonList = new ArrayList<>();
static List<Integer> foodCounterList = new ArrayList<>();
public void listPopulate() {
theTextFields.add(soupField);
theTextFields.add(guacField);
theTextFields.add(empanadaField);
theTextFields.add(jalapenoField);
theTextFields.add(quesaField);
theTextFields.add(choriField);
theTextFields.add(seafoodField);
theTextFields.add(avocadoField);
theTextFields.add(burritoField);
theTextFields.add(chilliField);
theTextFields.add(churrosField);
theTextFields.add(pastelField);
theTextFields.add(brownieField);
theTextFields.add(strawbField);
theTextFields.add(vanillaField);
theTextFields.add(colaField);
theTextFields.add(dietColaField);
theTextFields.add(mojitoField);
theTextFields.add(blueMargField);
theTextFields.add(redMargField);
theTextFields.add(bottleWaterField);
theTextFields.add(tapWaterField);
for (int i = 0; i< theTextFields.size(); i++) {
theTextFields.get(i).setText("0");
}
minusButtonList.add(soupMinus);
minusButtonList.add(guacMinus);
minusButtonList.add(empanadaMinus);
minusButtonList.add(jalapenoMinus);
minusButtonList.add(quesaMinus);
minusButtonList.add(choriMinus);
minusButtonList.add(seafoodMinus);
minusButtonList.add(avocadoMinus);
minusButtonList.add(burritoMinus);
minusButtonList.add(chilliMinus);
minusButtonList.add(churrosMinus);
minusButtonList.add(pastelMinus);
minusButtonList.add(brownieMinus);
minusButtonList.add(strawbMinus);
minusButtonList.add(vanillaMinus);
minusButtonList.add(colaMinus);
minusButtonList.add(dietColaMinus);
minusButtonList.add(mojitoMinus);
minusButtonList.add(blueMargMinus);
minusButtonList.add(redMargMinus);
minusButtonList.add(bottleWaterMinus);
minusButtonList.add(tapWaterMinus);
/**
plusButtonList[0] = soupPlus;
plusButtonList[1] = guacPlus;
plusButtonList[2] = empanadaPlus;
plusButtonList[3] = soupPlus;
plusButtonList[4] = soupPlus;
plusButtonList[5] = soupPlus;
plusButtonList[6] = soupPlus;
plusButtonList[7] = soupPlus;
plusButtonList[8] = soupPlus;
plusButtonList[9] = soupPlus;
plusButtonList[10] = soupPlus;
plusButtonList[11] = soupPlus;
plusButtonList[12] = soupPlus;
plusButtonList[13] = soupPlus;
plusButtonList[14] = soupPlus;
plusButtonList[15] = soupPlus;
plusButtonList[16] = soupPlus;
plusButtonList[17] = soupPlus;
plusButtonList[18] = soupPlus;
plusButtonList[19] = soupPlus;
plusButtonList[20] = soupPlus;
plusButtonList[21] = soupPlus;
plusButtonList[22] = soupPlus;
plusButtonList.add(soupPlus);
plusButtonList.add(guacPlus);
plusButtonList.add(empanadaPlus);
plusButtonList.add(jalapenoPlus);
plusButtonList.add(quesaPlus);
plusButtonList.add(choriPlus);
plusButtonList.add(seafoodPlus);
plusButtonList.add(avocadoPlus);
plusButtonList.add(burritoPlus);
plusButtonList.add(chilliPlus);
plusButtonList.add(churrosPlus);
plusButtonList.add(pastelPlus);
plusButtonList.add(browniePlus);
plusButtonList.add(strawbPlus);
plusButtonList.add(vanillaPlus);
plusButtonList.add(colaPlus);
plusButtonList.add(dietColaPlus);
plusButtonList.add(mojitoPlus);
plusButtonList.add(blueMargPlus);
plusButtonList.add(redMargPlus);
plusButtonList.add(bottleWaterPlus);
plusButtonList.add(tapWaterPlus);
**/
foodCounterList.add(Integer.parseInt(soupField.getText()));
foodCounterList.add(Integer.parseInt(guacField.getText()));
foodCounterList.add(Integer.parseInt(empanadaField.getText()));
foodCounterList.add(Integer.parseInt(jalapenoField.getText()));
foodCounterList.add(Integer.parseInt(quesaField.getText()));
foodCounterList.add(Integer.parseInt(choriField.getText()));
foodCounterList.add(Integer.parseInt(seafoodField.getText()));
foodCounterList.add(Integer.parseInt(avocadoField.getText()));
foodCounterList.add(Integer.parseInt(burritoField.getText()));
foodCounterList.add(Integer.parseInt(chilliField.getText()));
foodCounterList.add(Integer.parseInt(churrosField.getText()));
foodCounterList.add(Integer.parseInt(pastelField.getText()));
foodCounterList.add(Integer.parseInt(brownieField.getText()));
foodCounterList.add(Integer.parseInt(strawbField.getText()));
foodCounterList.add(Integer.parseInt(vanillaField.getText()));
foodCounterList.add(Integer.parseInt(colaField.getText()));
foodCounterList.add(Integer.parseInt(dietColaField.getText()));
foodCounterList.add(Integer.parseInt(mojitoField.getText()));
foodCounterList.add(Integer.parseInt(blueMargField.getText()));
foodCounterList.add(Integer.parseInt(redMargField.getText()));
foodCounterList.add(Integer.parseInt(bottleWaterField.getText()));
foodCounterList.add(Integer.parseInt(tapWaterField.getText()));
}
private void checkIfIncrement() {
for (int i = 0; i < theTextFields.size(); i++) {
setButtonActionsForFoodItem(plusButtonList.get(i), theTextFields.get(i));
}
}
private void setButtonActionsForFoodItem(Button plusButton, TextField textField) {
plusButton.setOnAction(event -> {
int currentValue = Integer.parseInt(textField.getText());
textField.setText(String.valueOf(++currentValue));
});
}
/**for(int i =0; i < plusButtonList.size(); i++) {
int counter = i;
plusButtonList.get(i).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
int currentCountVal = foodCounterList.get(counter);
theTextFields.get(counter).setText(Integer.toString(currentCountVal+ 1));
foodCounterList.set(counter, currentCountVal+1);
}
});
}**/
@FXML
private void handleCloseProgram(ActionEvent event) {
stage = (Stage)((Button)event.getSource()).getScene().getWindow();
stage.close();
System.exit(1);
}
@FXML
private void handleMinimiseProgram(ActionEvent event) {
stage = (Stage)((Button)event.getSource()).getScene().getWindow();
stage.setIconified(true);
}
@FXML
public void handle(ActionEvent event) {
}
@FXML
void initialize() {
//ButtonAndCountLists.setCounters();
listPopulate();
//System.out.println(plusButtonList.get(21));
System.out.println(foodCounterList.get(1));
checkIfIncrement();
}
}
首先,我不明白你为什么要在这个解决方案中使用静态列表?在那种情况下,我认为它们应该被标记为私人的。
接下来,我认为该方法 "checkIfIncrement" 没有正确的名称来说明它正在做什么。从你的代码我想它应该被命名为 setButtonsListeners()?
您是否在 FXML 文件的 increment/decrement 按钮上放置了正确的 fx:id="exampleName" ?也许它导致了 NPE。请在此处粘贴带有错误堆栈跟踪的 fxml 文件。
所以我遇到了类似的问题。我试图根据从数组列表中单击哪个按钮来获取特定按钮来触发特定事件。这是我的解决方案,我首先尝试使用 .isPressed() 但它始终返回 false,因此我将其更改为 isArmed() 并且有效。也许你可以试试看?我的代码写在下面。
serveButtons 是包含所有不同按钮的数组列表。
orderContentsList 是包含要更改的文本区域的数组列表。
int which = 0;
for (int i = 0; i < serveButtons.size(); i++) {
if (serveButtons.get(i).isArmed()) {
which = i;
break;
}
}
orderContentsList.get(which).setText(null);
如果您需要有关 arrayLists 的更多信息,请告诉我。
我正在为餐厅菜单实现一个用户界面,它涉及大量递增和递减按钮,如下所示 - 请注意我使用 SceneBuilder 创建了 UI:
我已经尝试了很多方法来允许循环遍历每个按钮并在单击鼠标时更新它旁边的文本字段但是未能通过 arrayList 实现这一点 - 当我知道 arraylist 是成功填满按钮。 这是我尝试过的: 我有一个方法可以通过指定相应按钮的 fxid 来填充列表 -
plusButtonList.add(soupPlus);
plusButtonList.add(guacPlus);
plusButtonList.add(empanadaPlus);
plusButtonList.add(jalapenoPlus);
plusButtonList.add(quesaPlus);
然后我还有一个方法应该遍历每个按钮,设置 onaction 以从文本字段检索当前计数器值(它也在列表中,但相应的按钮和文本字段将是在相同的索引位置)此方法在控制器 class 的 'initialise' 中调用。
private void checkIfIncrement() {
for(int i = 0; i < 20; i++) {
int counter = i;
plusButtonList.get(i).setOnAction(e -> {
int currentCountVal = foodCounterList.get(counter);
theTextFields.get(counter).setText(Integer.toString(currentCountVal+ 1));
foodCounterList.set(counter, currentCountVal+1);
});
}
注意:foodCounterList 是包含每个文本字段当前值的整数列表。 'theTextFields' 是包含每个文本字段的列表。 更新:这是我的控制器 Class 在尝试实施 Matt 建议的改进后
package customer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Pagination;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
/**
* View for the customer.
* @author Arslan Nazir
*
*/
public class CustomerView {
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private ImageView imageview;
Stage stage;
@FXML
private Button soupPlus;
@FXML
private Button soupMinus;
@FXML
private TextField soupField;
@FXML
private Button guacMinus;
@FXML
private Button guacPlus;
@FXML
private TextField guacField;
@FXML
private Font x11322;
@FXML
private Button empanadaMinus;
@FXML
private TextField empanadaField;
@FXML
private Button empanadaPlus;
@FXML
private Button jalapenoMinus;
@FXML
private Button jalapenoPlus;
@FXML
private TextField jalapenoField;
@FXML
private Button quesaMinus;
@FXML
private Button quesaPlus;
@FXML
private TextField quesaField;
@FXML
private Button choriMinus;
@FXML
private Button choriPlus;
@FXML
private TextField choriField;
@FXML
private Button seafoodMinus;
@FXML
private Button seafoodPlus;
@FXML
private TextField seafoodField;
@FXML
private Button avocadoMinus;
@FXML
private Button avocadoPlus;
@FXML
private TextField avocadoField;
@FXML
private Button burritoMinus;
@FXML
private Button burritoPlus;
@FXML
private TextField burritoField;
@FXML
private Button chilliMinus;
@FXML
private Button chilliPlus;
@FXML
private TextField chilliField;
@FXML
private Font x113221;
@FXML
private Button churrosMinus;
@FXML
private Button churrosPlus;
@FXML
private TextField churrosField;
@FXML
private Button pastelMinus;
@FXML
private Button pastelPlus;
@FXML
private TextField pastelField;
@FXML
private Button brownieMinus;
@FXML
private Button browniePlus;
@FXML
private TextField brownieField;
@FXML
private Button strawbMinus;
@FXML
private Button strawbPlus;
@FXML
private TextField strawbField;
@FXML
private Button vanillaMinus;
@FXML
private Button vanillaPlus;
@FXML
private TextField vanillaField;
@FXML
private Font x11321;
@FXML
private Button colaPlus;
@FXML
private Button colaMinus;
@FXML
private TextField colaField;
@FXML
private Button dietColaMinus;
@FXML
private Button dietColaPlus;
@FXML
private TextField dietColaField;
@FXML
private Button mojitoMinus;
@FXML
private Button mojitoPlus;
@FXML
private TextField mojitoField;
@FXML
private Button blueMargMinus;
@FXML
private Button blueMargPlus;
@FXML
private TextField blueMargField;
@FXML
private Button redMargMinus;
@FXML
private Button redMargPlus;
@FXML
private TextField redMargField;
@FXML
private Button bottleWaterMinus;
@FXML
private Button bottleWaterPlus;
@FXML
private TextField bottleWaterField;
@FXML
private Button tapWaterMinus;
@FXML
private Button tapWaterPlus;
@FXML
private TextField tapWaterField;
@FXML
private Button closeprogram;
@FXML
private Button minimiseprogram;
@FXML
private VBox bgone;
@FXML
private Button callwaiter;
@FXML
private TextField summaryField;
static List<TextField> theTextFields = new ArrayList<>();
ArrayList<Button> plusButtonList = new ArrayList<>(Arrays.asList(soupPlus, guacPlus));
static List<Button> minusButtonList = new ArrayList<>();
static List<Integer> foodCounterList = new ArrayList<>();
public void listPopulate() {
theTextFields.add(soupField);
theTextFields.add(guacField);
theTextFields.add(empanadaField);
theTextFields.add(jalapenoField);
theTextFields.add(quesaField);
theTextFields.add(choriField);
theTextFields.add(seafoodField);
theTextFields.add(avocadoField);
theTextFields.add(burritoField);
theTextFields.add(chilliField);
theTextFields.add(churrosField);
theTextFields.add(pastelField);
theTextFields.add(brownieField);
theTextFields.add(strawbField);
theTextFields.add(vanillaField);
theTextFields.add(colaField);
theTextFields.add(dietColaField);
theTextFields.add(mojitoField);
theTextFields.add(blueMargField);
theTextFields.add(redMargField);
theTextFields.add(bottleWaterField);
theTextFields.add(tapWaterField);
for (int i = 0; i< theTextFields.size(); i++) {
theTextFields.get(i).setText("0");
}
minusButtonList.add(soupMinus);
minusButtonList.add(guacMinus);
minusButtonList.add(empanadaMinus);
minusButtonList.add(jalapenoMinus);
minusButtonList.add(quesaMinus);
minusButtonList.add(choriMinus);
minusButtonList.add(seafoodMinus);
minusButtonList.add(avocadoMinus);
minusButtonList.add(burritoMinus);
minusButtonList.add(chilliMinus);
minusButtonList.add(churrosMinus);
minusButtonList.add(pastelMinus);
minusButtonList.add(brownieMinus);
minusButtonList.add(strawbMinus);
minusButtonList.add(vanillaMinus);
minusButtonList.add(colaMinus);
minusButtonList.add(dietColaMinus);
minusButtonList.add(mojitoMinus);
minusButtonList.add(blueMargMinus);
minusButtonList.add(redMargMinus);
minusButtonList.add(bottleWaterMinus);
minusButtonList.add(tapWaterMinus);
/**
plusButtonList[0] = soupPlus;
plusButtonList[1] = guacPlus;
plusButtonList[2] = empanadaPlus;
plusButtonList[3] = soupPlus;
plusButtonList[4] = soupPlus;
plusButtonList[5] = soupPlus;
plusButtonList[6] = soupPlus;
plusButtonList[7] = soupPlus;
plusButtonList[8] = soupPlus;
plusButtonList[9] = soupPlus;
plusButtonList[10] = soupPlus;
plusButtonList[11] = soupPlus;
plusButtonList[12] = soupPlus;
plusButtonList[13] = soupPlus;
plusButtonList[14] = soupPlus;
plusButtonList[15] = soupPlus;
plusButtonList[16] = soupPlus;
plusButtonList[17] = soupPlus;
plusButtonList[18] = soupPlus;
plusButtonList[19] = soupPlus;
plusButtonList[20] = soupPlus;
plusButtonList[21] = soupPlus;
plusButtonList[22] = soupPlus;
plusButtonList.add(soupPlus);
plusButtonList.add(guacPlus);
plusButtonList.add(empanadaPlus);
plusButtonList.add(jalapenoPlus);
plusButtonList.add(quesaPlus);
plusButtonList.add(choriPlus);
plusButtonList.add(seafoodPlus);
plusButtonList.add(avocadoPlus);
plusButtonList.add(burritoPlus);
plusButtonList.add(chilliPlus);
plusButtonList.add(churrosPlus);
plusButtonList.add(pastelPlus);
plusButtonList.add(browniePlus);
plusButtonList.add(strawbPlus);
plusButtonList.add(vanillaPlus);
plusButtonList.add(colaPlus);
plusButtonList.add(dietColaPlus);
plusButtonList.add(mojitoPlus);
plusButtonList.add(blueMargPlus);
plusButtonList.add(redMargPlus);
plusButtonList.add(bottleWaterPlus);
plusButtonList.add(tapWaterPlus);
**/
foodCounterList.add(Integer.parseInt(soupField.getText()));
foodCounterList.add(Integer.parseInt(guacField.getText()));
foodCounterList.add(Integer.parseInt(empanadaField.getText()));
foodCounterList.add(Integer.parseInt(jalapenoField.getText()));
foodCounterList.add(Integer.parseInt(quesaField.getText()));
foodCounterList.add(Integer.parseInt(choriField.getText()));
foodCounterList.add(Integer.parseInt(seafoodField.getText()));
foodCounterList.add(Integer.parseInt(avocadoField.getText()));
foodCounterList.add(Integer.parseInt(burritoField.getText()));
foodCounterList.add(Integer.parseInt(chilliField.getText()));
foodCounterList.add(Integer.parseInt(churrosField.getText()));
foodCounterList.add(Integer.parseInt(pastelField.getText()));
foodCounterList.add(Integer.parseInt(brownieField.getText()));
foodCounterList.add(Integer.parseInt(strawbField.getText()));
foodCounterList.add(Integer.parseInt(vanillaField.getText()));
foodCounterList.add(Integer.parseInt(colaField.getText()));
foodCounterList.add(Integer.parseInt(dietColaField.getText()));
foodCounterList.add(Integer.parseInt(mojitoField.getText()));
foodCounterList.add(Integer.parseInt(blueMargField.getText()));
foodCounterList.add(Integer.parseInt(redMargField.getText()));
foodCounterList.add(Integer.parseInt(bottleWaterField.getText()));
foodCounterList.add(Integer.parseInt(tapWaterField.getText()));
}
private void checkIfIncrement() {
for (int i = 0; i < theTextFields.size(); i++) {
setButtonActionsForFoodItem(plusButtonList.get(i), theTextFields.get(i));
}
}
private void setButtonActionsForFoodItem(Button plusButton, TextField textField) {
plusButton.setOnAction(event -> {
int currentValue = Integer.parseInt(textField.getText());
textField.setText(String.valueOf(++currentValue));
});
}
/**for(int i =0; i < plusButtonList.size(); i++) {
int counter = i;
plusButtonList.get(i).setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
int currentCountVal = foodCounterList.get(counter);
theTextFields.get(counter).setText(Integer.toString(currentCountVal+ 1));
foodCounterList.set(counter, currentCountVal+1);
}
});
}**/
@FXML
private void handleCloseProgram(ActionEvent event) {
stage = (Stage)((Button)event.getSource()).getScene().getWindow();
stage.close();
System.exit(1);
}
@FXML
private void handleMinimiseProgram(ActionEvent event) {
stage = (Stage)((Button)event.getSource()).getScene().getWindow();
stage.setIconified(true);
}
@FXML
public void handle(ActionEvent event) {
}
@FXML
void initialize() {
//ButtonAndCountLists.setCounters();
listPopulate();
//System.out.println(plusButtonList.get(21));
System.out.println(foodCounterList.get(1));
checkIfIncrement();
}
}
首先,我不明白你为什么要在这个解决方案中使用静态列表?在那种情况下,我认为它们应该被标记为私人的。 接下来,我认为该方法 "checkIfIncrement" 没有正确的名称来说明它正在做什么。从你的代码我想它应该被命名为 setButtonsListeners()? 您是否在 FXML 文件的 increment/decrement 按钮上放置了正确的 fx:id="exampleName" ?也许它导致了 NPE。请在此处粘贴带有错误堆栈跟踪的 fxml 文件。
所以我遇到了类似的问题。我试图根据从数组列表中单击哪个按钮来获取特定按钮来触发特定事件。这是我的解决方案,我首先尝试使用 .isPressed() 但它始终返回 false,因此我将其更改为 isArmed() 并且有效。也许你可以试试看?我的代码写在下面。
serveButtons 是包含所有不同按钮的数组列表。 orderContentsList 是包含要更改的文本区域的数组列表。
int which = 0;
for (int i = 0; i < serveButtons.size(); i++) {
if (serveButtons.get(i).isArmed()) {
which = i;
break;
}
}
orderContentsList.get(which).setText(null);
如果您需要有关 arrayLists 的更多信息,请告诉我。