我对javafx很新。我看到了许多在屏幕之间切换的方法,但是在经过一些思考后它们不知何故无法工作,我想到了使用这种逻辑。在项目走得太远之前,我需要知道是否可以继续使用它。
@FXML
public void nextAfterPassangerButtonClicked() throws Exception {
MainScreenDatabaseHandler a = new MainScreenDatabaseHandler(getId(), getFirstName(), getLastName(), getOtherName(), getSexSelection(), getMobileNumber(), getEmergencyContact(), getHomeAdress());
//send collected data to database
passangerPaymentAnchorPane.getChildren().remove(0);
Node node = FXMLLoader.load(getClass().getResource("CargoPayment.fxml"));
passangerPaymentAnchorPane.getChildren().add(node);
}
//Handle All Menu Bar Buttons
@FXML
public void startPageBarButtonClicked() throws Exception {
passangerPaymentAnchorPane.getChildren().remove(0);
mainAnchor.getChildren().remove(0);
Node node = FXMLLoader.load(getClass().getResource("MainScreen.fxml"));
mainAnchor.getChildren().add(node);
}
public void allPassangersMenuBarButtonClicked() throws Exception {
passangerPaymentAnchorPane.getChildren().remove(0);
Node node = FXMLLoader.load(getClass().getResource("AllPassangersView.fxml"));
passangerPaymentAnchorPane.getChildren().add(node);
}
当我点击其中一个按钮时,它会删除当前场景并加载相关的fxml。 谢谢。 passangerPaymentAnchorPane的作用类似于其他fxml加载和卸载的母窗格
答案 0 :(得分:1)
主要方法没有任何问题,即有一些布局就像一个用于加载fxml文件的容器。它的孩子将被取代一些事件动作。我建议在项目增长之前重构代码,比如loadView(String fxmlFile)
方法,其中重复的“layout.remove - fxmlloader.load() - layout.add()”对被重构。此外,由于解析和加载fxml文件是一项昂贵的工作,所以请懒得并仅更新视图,如:
Pane cargoPaymentPane;
CargoPaymentController cargoPaymentController;
FXMLLoader fxmlLoader = new FXMLLoader();
public void loadCargoPaymentView(String newCargoData) {
if(cargoPaymentPane == null) {
cargoPaymentPane = fxmlLoader.load(getClass().getResource("CargoPayment.fxml").openStream());
cargoPaymentController = (CargoPaymentController) fxmlLoader.getController();
}
cargoPaymentController.updateView(newCargoData);
}
答案 1 :(得分:1)