将 EggView 添加到 _eggConta-6ren">
gpt4 book ai didi

java - 如何从多个嵌套 FXML 获取数据并调用其 Controller 上的方法

转载 作者:行者123 更新时间:2023-12-02 11:46:59 31 4
gpt4 key购买 nike

这是我的项目的样子:

Main.FXML

  • 锚定 Pane
    • VBox [鸡蛋容器] id="_eggContainer"
    • 按钮 [Add Egg] --> 将 EggView 添加到 _eggContainer
    • 按钮 [Print Eggs] --> 打印所有鸡蛋的属性
    • 按钮[Break Eggs] --> 对每个鸡蛋调用break方法

EggView.FXML

  • HBox
    • 标签:[Egg ID] 每次实例化 EggView 时都应递增
    • 文本字段:[鸡蛋颜色]

MainController.java

public class MainController implements Initializable {   

/***CONTROLS***/
@FXML private VBox _eggsContainer;
@FXML private Button _btnAddEgg;
@FXML private Button _btnPrintEggs;
@FXML private Button _btnBreakEggs;

@Override
public void initialize(URL location, ResourceBundle resources) {
_btnAddEgg.setOnMouseClicked(e -> {
doAddEgg();
});
_btnPrintEggs.setOnMouseClicked(e -> {
doPrintAllEggs();
});
_btnBreakEggs.setOnMouseClicked(e -> {
doBreakAllEggs();
});
}

// Add an egg to _eggsContainer
private void doAddEgg() {
HBox eggControl = null;
try {
eggControl = FXMLLoader.load(getClass().getResource("EggView.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
_eggsContainer.getChildren().add(eggControl);
}

private void doPrintAllEggs() {
// prints ID and Color of each egg in _eggsContainer
}

private void doBreakAllEggs() {
// call the break method in EggController controller on each egg
}
}

EggController.java

public class EggController {

/***CONTROLS***/
@FXML private Label _eggID;
@FXML private TextField _eggColor;

public void doBreakEgg() {
// TODO: Break Egg
}

/****************************************************************
********** ACCESSORS ************
****************************************************************/
public String getEggID() {
return _eggID.getText();
}

public String setEggID(String id) {
return _eggID.setText(id);
}

public String getEggColor() {
return _eggColor.getText();
}

}

现在我如何从主 Controller 访问每个鸡蛋数据和方法?无需为每个鸡蛋提供 ID 并在 MainController 中引用它。

最佳答案

加载EggView.fxml时从FXMLLoader获取 Controller ,并将它们放入列表中。 (顺便说一句,不要使用鼠标监听器来处理按钮操作。)

public class MainController implements Initializable {   

/***CONTROLS***/
@FXML private VBox _eggsContainer;
@FXML private Button _btnAddEgg;
@FXML private Button _btnPrintEggs;
@FXML private Button _btnBreakEggs;

private final List<EggController> eggs = new ArrayList<>();

@Override
public void initialize(URL location, ResourceBundle resources) {
_btnAddEgg.setOnAction(e -> {
doAddEgg();
});
_btnPrintEggs.setOnAction(e -> {
doPrintAllEggs();
});
_btnBreakEggs.setOnAction(e -> {
doBreakAllEggs();
});
}

// Add an egg to _eggsContainer
private void doAddEgg() {
HBox eggControl = null;
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("EggView.fxml"));
eggControl = loader.load();
EggController controller = loader.getController();
eggs.add(controller);
} catch (IOException e) {
e.printStackTrace();
}
_eggsContainer.getChildren().add(eggControl);
}

private void doPrintAllEggs() {
// prints ID and Color of each egg in _eggsContainer
for (EggController controller : eggs) {
System.out.println(controller.getEggId() + " : " + controller.getEggColor());
}
}

private void doBreakAllEggs() {
eggs.forEach(EggController::doBreakEgg);
}
}

关于java - 如何从多个嵌套 FXML 获取数据并调用其 Controller 上的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48130279/

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