验证父节点中的内部/顶部节点



我有一个AnchorPane里面有相当数量的容器(其中大多数都是AnchorPane): MainAnchorPane总是被设置为顶部的AnchorPane,它有多个AnchorPane作为子(让我们说这些是AnchorPane2和AnchorPane3)。

期待找到一种方法来识别哪个anchorPane在主容器内,我试着做以下事情:

if (mainAnchorPane.getChildren().contains(anchorPane2)) 
{ System.out.println("anchorPane2 is opened here"); }
else if (mainAnchorPane.getChildren().contains(anchorPane3))
{System.out.println("anchorPane3 is opened here");}
else {System.out.println("no anchorPane found");}

虽然,这个方法不起作用。

我还试图比较其他类的anchorPane,但这给了我NullPointerException(即使与anchorPane打开是系统的另一部分-例如,我有多个MainAnchorPanes可以处理相同的anchorPane2和3同时打开-):

if (mainAnchorPane.getChildren().contains(anotherClass.anchorPane2)) 
{System.out.println("anchorPane2 is opened here");}
else {System.out.println("no anchorPane found");}

这将是我首选的方法,但正如我提到的,由于某种原因,它会在if语句的生命周期内给我一个空异常。

最后,我尝试了最后的努力,通过在MainAnchorPane中创建一个节点列表,并将每个节点与目标节点(anchorPane2或anchorPane3)进行比较,以确定当前在MainAnchorPane中显示的是哪个anchorPane:

for (Node node: getAllNodes(mainAnchorPane))
{
System.out.println(node);
// in the place of equals() I've tried contains() as well
if (node.equals(anchorPane2)) {
System.out.println("ap2 is here");
}
else {System.out.println("ap2 is not here");}
}
public static ArrayList<Node> nodes = new ArrayList<Node>();
public static ArrayList<Node> getAllNodes(Parent root) {
addAllDescendents(root, nodes);
return nodes;
}
private static void addAllDescendents(Parent parent, ArrayList<Node> nodes) {
for (Node node : parent.getChildrenUnmodifiable()) {
nodes.add(node);
if (node instanceof Parent)
addAllDescendents((Parent)node, nodes);
}
}

这些方法都不起作用。我甚至进一步检查了我的ifs是否捕获了AnchorPanes中的按钮和标签节点,这些节点是该窗格的节点,尽管条件总是返回为false,表明节点不在那里(这不是真的,因为if显示在System.out中的所有中)。println(nodes, mainAnchorPane. getchildren (), and getAllNodes(mainAnchorPane)))

如果你有什么想法,我将不胜感激。

编辑:应该像 一样简单
if (mainAnchorPane.getChildren().contains(node))

但这不起作用

命名规范被简化,目的是为了更好的解释。

在高水平上你的代码看起来很好。话虽如此,你还是需要做一些相当大的修改/清理。

  • 不需要静态数组列表
  • 不需要检入for循环。…

我很快尝试了你的代码与一些变化,它工作得很好。试着做相应的修改。即使你有问题……那么你可能在其他地方做错了什么。

import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.ArrayList;
import java.util.List;
public class LayoutDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
AnchorPane anchorPane2 = new AnchorPane(new StackPane());
AnchorPane anchorPane3 = new AnchorPane(new StackPane());
// Change the internal node to anchorPane3 to get "not here" output
AnchorPane mainAnchorPane = new AnchorPane(new StackPane(new Pane(new VBox(anchorPane2, new Pane()), new Pane())), new Pane());
Pane root = new Pane(mainAnchorPane);
Scene scene = new Scene(root, 400, 400);
stage.setScene(scene);
stage.show();
if (getAllNodes(mainAnchorPane).contains(anchorPane2)) {
System.out.println("ap2 is here");
} else {
System.out.println("ap2 is not here");
}
}
public static List<Node> getAllNodes(Parent root) {
List<Node> nodes = new ArrayList<Node>();
addAllDescendents(root, nodes);
return nodes;
}
private static void addAllDescendents(Parent parent, List<Node> nodes) {
for (Node node : parent.getChildrenUnmodifiable()) {
nodes.add(node);
if (node instanceof Parent)
addAllDescendents((Parent) node, nodes);
}
}
}