我有两个阶段。在一个阶段(主阶段)中,我有一个Borderpane。在此主阶段中单击“创建订单”按钮时,我将.fxml文件(“新订单”)加载到边框的中央。此“新订单” fxml文件具有一些文本字段。现在,我点击第二个按钮以打开第二阶段(新客户)。主舞台仍在后台打开。在“新客户”阶段,我可以在文本字段中填写文本。当我在“保存”按钮上关闭此阶段时,我希望在主阶段中,Borderpane中心的文本字段(因此在“ New Order fxml”中)具有打开的窗口中的文本。
对我来说,好像我需要再次初始化“ New Order .fxml”。但这不起作用,因为它在后台打开了吗?
我尝试将主舞台的中心设置为null,然后将“新订单”重新加载到中心。但是然后它没有出现。 但是,当我再次在主窗口中单击“创建订单”时,文本字段将填充正确的文本。但我不想再次单击。
如果您需要更多代码,请告诉我。
MainWindowController.java
将新的fxml加载到我的MainWindow中心的方法:
@FXML
public BorderPane testPane = new BorderPane();
@FXML
protected void createOrder(ActionEvent event) throws IOException {
BorderPane test1 = FXMLLoader.load(getClass().getResource("NewOrder.fxml"));
testPane.setCenter(test1);
}
NewOrderController.java
//方法打开新的舞台
private Stage newStage = new Stage();
@FXML
private void openNewCostumerWindow(ActionEvent event) {
try {
BorderPane root = FXMLLoader.load(getClass().getResource("NewCustomer.fxml"));
Scene scene = new Scene(root, 600, 250);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
newStage.setTitle("Neuer Kunde");
//Set that the main window can not be clicked
newStage.initModality(Modality.WINDOW_MODAL);
newStage.initOwner(newOrderPane.getScene().getWindow());
newStage.setResizable(false);
newStage.setScene(scene);
newStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
///在这里,我们从NewCustomer文本字段中获取文本,并将其设置为这些文本字段的文本。但这不起作用
@FXML
public void initialize() {
try {
nameTextField.setEditable(true);
nameTextField.setText(NewCustomerController.customerName);
streetTextField.setText(NewCustomerController.customerStreet);
cityTextField.setText(NewCustomerController.customerCity);
countryTextField.setText(NewCustomerController.customerCountry);
System.out.println(NewCustomerController.customerName);
System.out.println("TEST");
} catch (Exception e) {
}
}
NewCustomerController.java
@FXML
LinkedList<Customer> saveNewCustomer(ActionEvent event) throws IOException {
//creating new Customer
customerName = newNameTextField.getText();
customerStreet = newStreetTextField.getText();
customerCity = newCityTextField.getText();
customerCountry = newCountryTextField.getText();
Customer customer = new Customer (customerName, customerStreet, customerCity, customerCountry);
//add customer to the list
customerList.add(customer);
System.out.println(customerList);
//after saving close the stage
Stage stage = (Stage) quitButton.getScene().getWindow();
stage.close();
return customerList;
}