btn1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Enumeration<?> enumeration;
String model = e.getSource().toString();
if (model.equals("按前序遍历")) {
enumeration = root.preorderEnumeration();
} else if (model.equals("按后序遍历")) {
enumeration = root.postorderEnumeration();
} else if (model.equals("以广度优先遍历")) {
enumeration = root.breadthFirstEnumeration();
} else if (model.equals("以深度优先遍历")) {
enumeration = root.depthFirstEnumeration();
} else {
enumeration = root.children();// 遍历该节点的子节点
}
while(enumeration.hasMoreElements()) {//遍历节点枚举对象
DefaultMutableTreeNode node;//获得节点
node=(DefaultMutableTreeNode) enumeration.nextElement();
//根据节点级别输出占位符
for (int i = 0; i < node.getLevel(); i++) {
System.out.print("----");
}
System.out.println(node.getUserObject());
}
}
});
}
我对书上的代码进行了填充,预期效果是所有节点都可以遍历出来,结果只是遍历了第一级的节点,是我代码有错误还是哪里理解有偏差?