gpt4 book ai didi

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

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

这就是我的观点:

enter image description here

说明

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

问题

我不知道当用户单击“X”按钮时如何通知 CartWindowController,该按钮由“CartItemComponent” Controller 处理。如果有人能告诉我如何解决这个问题,我将非常感激。

这是我的 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 时,您可以调用方法或设置标志或触发事件,您的选择!您需要做的就是向 CartItemComponent 添加一个 CartWindowController 类型的参数,然后保存引用。

关于java - 在同一场景的另一个 JavaFX Controller 中进行更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54749935/

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