小编Tho*_*mas的帖子

使用循环中的按钮调用方法

我创建了一个名为gPaneCenter的gridPane,用于创建表.当我将一个项目添加到网格时,它将调用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++;
}
Run Code Online (Sandbox Code Playgroud)

'i'和'l'在开始时声明:

static int i = 0;
static int l = 1;
Run Code Online (Sandbox Code Playgroud)

在添加项目时会生成以下GUI:

在此输入图像描述

当我单击" - "按钮或"+"按钮时,我想减少数量并分别增加数量,但是当我点击任一按钮时出现以下错误:

Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: Index: 1, Size: …
Run Code Online (Sandbox Code Playgroud)

java javafx increment button

3
推荐指数
1
解决办法
88
查看次数

标签 统计

button ×1

increment ×1

java ×1

javafx ×1