【发布时间】:2021-11-18 19:26:15
【问题描述】:
我正在制作一个程序,您可以在其中创建成分并使用这些成分创建菜单。
我有一个控制器类,它反过来控制其他类。这个叫做 CucharitaGUI,它控制 InventoryModuleControllerGUI 和 MenuModuleControllerGUI。我在名为 InventoryManager(连接到 InventoryModuleControllerGUI)的类中有一个成分的 ArrayList 和另一个 ArrayList,我希望在 MenuModuleControllerGUI 类中有相同的成分对象,并且能够在不编辑原始 ArrayList(来自 InventoryManager 的那个)的情况下编辑它们。出于这个原因,我试图克隆它,但我收到了警告。
CucharitaGUI 类:
//Attributes
public UserManager userManager;
private StaffModuleControllerGUI staffModule;
public InventoryModuleControllerGUI inventoryModule;
public MenuModuleControllerGUI menuModule;
public OrderModuleControllerGUI orderModule;
//private InventoryManager inventoryManager;
private Stage loginStage;
//@FXML Attributes:
@FXML
private PasswordField pFLogin;
@FXML
private Button btnLogIn;
@FXML
private TextField txtUserLogin;
@FXML
private Pane loginPane;
@FXML
private Pane taskManagerPane;
public CucharitaGUI()
{
staffModule = new StaffModuleControllerGUI(this);
inventoryModule = new InventoryModuleControllerGUI(this);
menuModule = new MenuModuleControllerGUI(this);
orderModule = new OrderModuleControllerGUI(this);
userManager = new UserManager();
//inventoryManager = new InventoryManager();
loginStage = new Stage();
}
@FXML
private void openInventoryModule(ActionEvent event) throws IOException
{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("inventoryModule.fxml"));
fxmlLoader.setController(inventoryModule);
Parent root = fxmlLoader.load();
Scene scene = new Scene(root);
loginStage.setScene(scene);
loginStage.setTitle("InventoryModule");
loginStage.show();
inventoryModule.initializeTableView();
inventoryModule.initializeComboBox();
}
@FXML
void openMenuModule(ActionEvent event) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("menuModule.fxml"));
fxmlLoader.setController(menuModule);
Parent root = fxmlLoader.load();
Scene scene = new Scene(root);
loginStage.setScene(scene);
loginStage.setTitle("MenuModule");
loginStage.show();
menuModule.initializeTableView();
menuModule.initializeComboBox();
}
InventoryModuleController 类:
//Attributes
private Stage inventoryModuleStage;
private ObservableList<Ingredient> observableInventoryList;
private ObservableList<String> observableUnitsList;
public InventoryManager inventoryManager;
private CucharitaGUI cucharitaGUI;
public InventoryModuleControllerGUI(CucharitaGUI cucharitaGUI){
setInventoryModuleStage(new Stage());
inventoryModulePane = new Pane();
inventoryManager = new InventoryManager();
this.cucharitaGUI = cucharitaGUI;
}
InventoryManager 类:
public class InventoryManager {
public ArrayList<Ingredient> ingredients;
public InventoryManager()
{
ingredients = new ArrayList<Ingredient>();
}
public ArrayList<Ingredient> getIngredients()
{
return ingredients;
}
MenuModuleControllerGUI 类:
ArrayList<Ingredient> newIngredientsToPreview;
public MenuManager menuManager;
private CucharitaGUI cucharitaGUI;
private boolean dishNameReadyToCreate=false;
private boolean dishIngredientsReadyToCreate=false;
public MenuModuleControllerGUI(CucharitaGUI cucharitaGUI){
setMenuModuleStage(new Stage());
menuModulePane = new Pane();
menuManager = new MenuManager();
ArrayList<Ingredient>ingredientClone=cucharitaGUI.
inventoryModule.inventoryManager.getIngredients();
newIngredientsToPreview= (ArrayList<Ingredient>)ingredientClone.clone();
this.cucharitaGUI = cucharitaGUI;
}
我在这里收到警告:
newIngredientsToPreview= (ArrayList<Ingredient>)ingredientClone.clone();
上面写着Type safety: Unchecked cast from Object to ArrayList<Ingredient>
有人知道怎么解决吗?
【问题讨论】:
-
您可以阅读this 来完成您的任务。