gpt4 book ai didi

java - 如何在JTree java swing中显示BinaryTree

转载 作者:行者123 更新时间:2023-11-30 04:08:36 24 4
gpt4 key购买 nike

我有二叉树类,当然还有节点类

public class Node {
SomeClass somedata;
Node leftChld;
Node rightChld;
}

我需要在 JTree swing gui 组件中显示我的二进制文件。请给出执行此操作的示例。提前谢谢。

抱歉这个话题,我自己做的这是这里

 public void showInTree(DefaultMutableTreeNode rtCm,BNode rt){
if (rt!=null) {
StringBuilder str = new StringBuilder(String.valueOf(rt.numVal));
if (rt.chVal!='\0'){
str.append("--" + "(").append(String.valueOf(rt.chVal)).append(")");
}
DefaultMutableTreeNode newnode = new DefaultMutableTreeNode(str);
rtCm.add(newnode);
if (rt.leftBNode != null) {
showInTree(newnode, rt.leftBNode);
}
if (rt.rightBNode!=null) {
showInTree(newnode,rt.rightBNode);
}
}
}

最佳答案

这里是

 public void showInTree(DefaultMutableTreeNode rtCm,BNode rt){
if (rt!=null) {
StringBuilder str = new StringBuilder(String.valueOf(rt.numVal));
if (rt.chVal!='\0'){
str.append("--" + "(").append(String.valueOf(rt.chVal)).append(")");
}
DefaultMutableTreeNode newnode = new DefaultMutableTreeNode(str);
rtCm.add(newnode);
if (rt.leftBNode != null) {
showInTree(newnode, rt.leftBNode);
}
if (rt.rightBNode!=null) {
showInTree(newnode,rt.rightBNode);
}
}
}

关于java - 如何在JTree java swing中显示BinaryTree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20175715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com