如何使用子变量名称获取 StackPane 的子元素

huangapple go评论76阅读模式
英文:

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

问题

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

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

英文:
TabPane tabpaneSubs = new TabPane();
stackPane.getChildren().add(tabpaneSubs);

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

Instead of using .get(1) i want to use the variable's name : tabpaneSubs

How do i do that ?

答案1

得分: 1

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

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;
       }
     }
});
英文:

This should work simply because you already know your TabPane is a child at compile time:

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

More generally, you should use the id property to find specific things

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

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

huangapple
  • 本文由 发表于 2020年7月27日 17:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63112499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定