这是我的观点:

java - 在同一场景中的另一个JavaFX Controller中进行更改-LMLPHP

说明

整个窗口本身都在一个称为CartWindowController的控制器上运行,产品列表本身是一个名为CartItemComponent的JavaFX自定义控件,它具有自己的控制器。因此,当我以编程方式创建实例并填充VBox时,列表中的每个项目都有其自己的控制器实例。

问题

我无法弄清楚当用户单击“ CartItemComponent”控制器处理的“ X”按钮时如何通知CartWindowController。如果有人可以帮助我解决这个问题,我将非常感激。

这是整个Window的FXML外观:

CartWindow.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorPane" fx:id="parent" prefHeight="400.0" prefWidth="600.0" styleClass="pane" stylesheets="@../assets/userwindow.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ordermanagementsystem.controllers.CartWindowController">
    <children>
        <Button layoutX="25.0" layoutY="25.0" mnemonicParsing="false" onAction="#exitWindow" prefHeight="35.0" prefWidth="20.0" styleClass="back-button" />
        <Label layoutX="262.0" layoutY="17.0" styleClass="heading" text="Cart" />

        <ScrollPane hbarPolicy="NEVER" layoutY="97.0" prefHeight="315.0" prefWidth="600.0" styleClass="no-padding">
            <content>
              <AnchorPane prefWidth="600.0" styleClass="no-padding">
                <children>
                    <VBox fx:id="productList" layoutX="25.0" prefWidth="350.0" />
                    <AnchorPane layoutX="425.0" layoutY="25.0" prefWidth="150.0" styleClass="no-padding">
                        <children>
                            <Label layoutX="18.0" styleClass="heading-sub" text="Summary" />
                        <Label layoutX="1.0" layoutY="55.0" text="Gross:" />
                        <Label fx:id="grossTotal" alignment="CENTER_RIGHT" layoutX="47.0" layoutY="55.0" prefHeight="17.0" prefWidth="103.0" text="RM0.00" />
                        <Label layoutX="1.0" layoutY="75.0" text="Packaging:" />
                        <Label fx:id="packagingTotal" alignment="CENTER_RIGHT" layoutX="73.0" layoutY="75.0" prefHeight="17.0" prefWidth="77.0" text="RM0.00" />
                        <Label layoutX="1.0" layoutY="95.0" text="Total:" />
                        <Label fx:id="total" alignment="CENTER_RIGHT" layoutX="40.0" layoutY="95.0" prefHeight="17.0" prefWidth="110.0" styleClass="green-text" text="RM0.00" />
                        <Button layoutY="125.0" mnemonicParsing="false" onAction="#checkout" prefHeight="39.0" prefWidth="150.0" styleClass="action-button" text="Check Out" />
                        </children>
                    </AnchorPane>
                </children>
              </AnchorPane>
            </content>
        </ScrollPane>
    </children>
</AnchorPane>


CartWindowController.java

package ordermanagementsystem.controllers;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import ordermanagementsystem.cart.CartState;
import ordermanagementsystem.orders.models.OrderItem;
import ordermanagementsystem.viewcomponents.CartItemComponent;

public class CartWindowController extends ViewController implements Initializable {

    @FXML
    public Parent parent;

    @FXML
    private VBox productList;

    @FXML
    private Label grossTotal;

    @FXML
    private Label packagingTotal;

    @FXML
    private Label total;

    private CartState cartState;

    @FXML
    private void exitWindow(ActionEvent event) {
        try {
            this.openPage(this.parent, "MainWindow.fxml");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @FXML
    private void checkout(ActionEvent event) {

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        this.cartState = CartState.getInstance();

        for (OrderItem item : this.cartState.getItems()) {
            this.productList.getChildren().add(new CartItemComponent(item));
        }
    }

}


CartItemComponent.java:

package ordermanagementsystem.viewcomponents;

import java.io.IOException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import ordermanagementsystem.DialogBox;
import ordermanagementsystem.cart.CartState;
import ordermanagementsystem.orders.models.OrderItem;

public class CartItemComponent extends AnchorPane {

    @FXML
    private AnchorPane frame;

    @FXML
    private TextField quantity;

    @FXML
    private Label total;

    @FXML
    private Label productName;

    private OrderItem item;

    private CartState cartState;

    public CartItemComponent(OrderItem item) {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/ordermanagementsystem/views/components/CartItemComponent.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);

        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            exception.getStackTrace();
        }

        this.cartState = CartState.getInstance();
        this.item = item;

        this.setQuantity(1);
        this.quantity.setEditable(false);

        this.productName.setText(this.item.getProduct().getName());
    }

    private void setQuantity(int quantity) {
        this.quantity.setText(String.valueOf(quantity));
        this.item.setQuantity(quantity);
        this.cartState.updateItem(this.item);
        this.total.setText("RM" + String.format("%.2f", item.getProduct().getPrice() * quantity));
    }

    private int getQuantity() {
        return Integer.parseInt(this.quantity.getText());
    }

    private void updateSummary() {

    }

    @FXML
    private void add(ActionEvent event) {
        int quantity = this.getQuantity();

        if (quantity == 99) {
            DialogBox.showValidationDialog("The quantity cannot be over 99.");
        } else {
            this.setQuantity(quantity + 1);
        }
    }

    @FXML
    private void substract(ActionEvent event) {
        int quantity = this.getQuantity();

        if (quantity == 1) {
            DialogBox.showValidationDialog("The quantity cannot be below 1.");
        } else {
            this.setQuantity(quantity - 1);
        }
    }
}

最佳答案

您可以尝试将CartWindowController传递到CartItemComponent构造函数中,然后在需要通知CartWindowController时,调用方法或设置标志或触发事件,您可以选择!您需要做的就是为您的CartWindowController添加一个CartItemComponent类型的参数,然后保存引用。

10-06 05:29