此问题是,当我单击itemFXML时,我在MainFXML中添加了itemFXML。 其控制器将调用主控制器中的方法来更改主UI 但是不起作用 波纹管是我的源代码 主控制器(lblMoney.setText(String.valueOf(position))什么也没发生)
public class ThanhtoanController implements Initializable {
@FXML
private Label lblMoney;
@FXML
private VBox vbxItemList;
@FXML
private JFXButton btnThanhToan;
int money = 3;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
Platform.runLater(new Runnable() {
@Override
public void run() {
vbxItemList.getChildren().clear();
Node[] nodes = new Node[5];
for (int i = 0; i < nodes.length; i++) {
try {
FXMLLoader lLoader = new FXMLLoader(ItemListController.class.getResource("itemList.fxml"));
nodes[i] = lLoader.load();
vbxItemList.getChildren().add(nodes[i]);
ItemListController controller = lLoader.getController();
controller.setData();
lblMoney.setText(String.valueOf(money));
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
public void test(int position){
lblMoney.setText(String.valueOf(position)); //This problem
}
ItemController
public class ItemListController implements Initializable {
@FXML
private JFXButton btnDelete;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
}
public void deleteItem(ActionEvent event){
FXMLLoader fXMLLoader =new FXMLLoader(ThanhtoanController.class.getResource("thanhtoan.fxml"));
try {
fXMLLoader.load();
ThanhtoanController controller = fXMLLoader.getController();
controller.test(10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
我该怎么办?你能为我解释一下这个问题吗?谢谢大家阅读