如何使用 child 的变量名获取 StackPane 的 child

How to get StackPane's child using the child's variable name

TabPane tabpaneSubs = new TabPane();
stackPane.getChildren().add(tabpaneSubs);

button.setOnAction( actionEvent -> {
     stackPane.getChildren().get(1).toFront();
}

我不想使用 .get(1),而是使用变量的名称:tabpaneSubs

我该怎么做?

这应该可以工作,因为你已经知道你的 TabPane 在编译时是一个 child:

button.setOnAction( actionEvent -> {
     tabpaneSubs.toFront();
});

更一般地说,您应该使用 id 属性 来查找特定的东西

tabpaneSubs.setId("my-tab-pane");

button.setOnAction( actionEvent -> {
     for(Node node : stackPane.getChildren()) {
       if("my-tab-name".equals(node.getId()){
         node.toFront();
         return;
       }
     }
});