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

huangapple 未分类评论45阅读模式
英文:

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();
}

英文:
  1. TabPane tabpaneSubs = new TabPane();
  2. stackPane.getChildren().add(tabpaneSubs);
  3. button.setOnAction( actionEvent -> {
  4. stackPane.getChildren().get(1).toFront();
  5. }

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

How do i do that ?

答案1

得分: 1

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

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

更一般地,你应该使用 id 属性来找到特定的元素:

  1. tabpaneSubs.setId("my-tab-pane");
  2. button.setOnAction(actionEvent -> {
  3. for(Node node : stackPane.getChildren()) {
  4. if("my-tab-name".equals(node.getId())){
  5. node.toFront();
  6. return;
  7. }
  8. }
  9. });
英文:

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

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

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

  1. tabpaneSubs.setId("my-tab-pane");
  2. button.setOnAction( actionEvent -> {
  3. for(Node node : stackPane.getChildren()) {
  4. if("my-tab-name".equals(node.getId()){
  5. node.toFront();
  6. return;
  7. }
  8. }
  9. });

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

发表评论

匿名网友

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

确定