[英]Calling method with a button in a loop
I have created a gridPane called gPaneCenter which is designed to create a table. 我创建了一个名为gPaneCenter的gridPane,用于创建表。 When I add an item to the grid it will call the mehtod addRow(), please see below:
当我将一个项目添加到网格时,它将调用mehtod addRow(),请参阅下面的内容:
public static void addRow() {
System.out.println("Test");
Label prodDes = new Label(String.valueOf(Controller.shoppingCart.getOrder(i).getProduct().getDescription()));
Label prodPrice = new Label(String.valueOf(Controller.shoppingCart.getOrder(i).getProduct().getUnitPrice()));
Label prodQuant = new Label(String.valueOf(Controller.shoppingCart.getOrder(i).getQuantity()));
Label prodCost = new Label(String.valueOf(Controller.shoppingCart.getOrder(i).getCost()));
HBox quantityChanger = new HBox();
Button decreaseQuantBut = new Button("-");
Button increaseQuantBut = new Button("+");
decreaseQuantBut.setOnAction(e -> Controller.shoppingCart.getOrder(i).decreaseQuantity());
increaseQuantBut.setOnAction(e -> Controller.shoppingCart.getOrder(i).increaseQuantity());
quantityChanger.getChildren().addAll(decreaseQuantBut,prodQuant,increaseQuantBut);
View.gPaneCenter.addRow(l, prodDes,prodPrice,quantityChanger,prodCost);
l++;
i++;
}
'i' and 'l' are declared at the start: 'i'和'l'在开始时声明:
static int i = 0;
static int l = 1;
Which produces the following GUI when an item is added: 在添加项目时会生成以下GUI:
When I click the "-" button or "+" button I would like to decrease the quantity and increase the quantity respectively however I get the following error when I click either button: 当我单击“ - ”按钮或“+”按钮时,我想减少数量并分别增加数量,但是当我点击任一按钮时出现以下错误:
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at model.Cart.getOrder(Cart.java:59)
Which suggests it cannot find order however it can successfully find the order to print the following: prodDes, prodPrice and prodQuant. 这表明它无法找到订单,但它可以成功找到打印以下内容的订单:prodDes,prodPrice和prodQuant。
Method of called the order in class Cart, this is also (Cart.java.59): 在类Cart中调用命令的方法,这也是(Cart.java.59):
public Order getOrder(int i) {
return contents.get(i);
}
Method of increasing and decreasing the quantity in class Order: 增加和减少课程数量的方法顺序:
public void increaseQuantity() {
quantity++;
}
public void decreaseQuantity() {
quantity--;
}
The methods for used for getting the prodQuant and prodCost (prodDes and prodUnitPrice are through getProduct() ): 用于获取prodQuant和prodCost的方法(prodDes和prodUnitPrice是通过getProduct()):
public int getCost() {
return quantity * item.getUnitPrice();
}
public Product getProduct() {
return item;
}
public int getQuantity() {
return quantity;
}
You use i
to identify a certain row, but the value of i
changes when you add more rows. 您使用
i
来标识某个行,但是当您添加更多行时, i
的值会更改。
public static void addRow() {
int rowNum = i; //solution: a local variable to keep current i value
.....
decreaseQuantBut.setOnAction(e -> Controller.shoppingCart.getOrder(rowNum).decreaseQuantity());
increaseQuantBut.setOnAction(e -> Controller.shoppingCart.getOrder(rowNum).increaseQuantity());
.....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.