gpt4 book ai didi

java - Java 中的两个 JTree 相等/赋值

转载 作者:行者123 更新时间:2023-12-01 09:46:57 24 4
gpt4 key购买 nike

第 1 步 - 首先,我创建第一个 Jtree。

第 2 步 - 然后使用这个 Jtree 创建了一组 JTree,并将一些计算出的权重值添加到 HashMap 中。

Step3 - 然后我从这个 HashMap 中选择一个 JTree。

第 4 步 - 现在我想从第 2 步到第 3 步重复此操作。

在重复之前,我只想选取选定的树用于将来的过程。但它需要最终的 HashMap 添加树来收获过程。我更新了同一棵树并添加到 HashMap 中。

        public class JavaTree extends JFrame {

public static HashMap<String, DefaultMutableTreeNode> nodeReg = new HashMap<String, DefaultMutableTreeNode>();

public static void main(String[] args) {

}

public JTree method1() {

HashMap<JTree, Double> value = new HashMap<JTree, Double>();

//create tree root
DefaultMutableTreeNode Treeroot = new DefaultMutableTreeNode(new NodeInfor3(rootNode,x1));

//create tree
JTree firstTree = new JTree(Treeroot);
add(firstTree);

// create the child nodes
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(new NodeInfor3(nodeName1, x2));
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(new NodeInfor3(nodeName2), x3));
nodeReg.put(rootNode, Treeroot);
nodeReg.put(nodeName1), node1);
nodeReg.put(nodeName2, node2);

Treeroot.add(node1);
Treeroot.add(node2);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("First JTree");
this.pack();
this.setVisible(true);



for (Map.Entry m1 : newNodeSet.entrySet()) {-----forloop X

for (Map.Entry m3 : TargetNodes.entrySet()) { -----forloop Y

DefaultMutableTreeNode targetNode = nodeReg.get(nodeName3);

targetNode.add(newAddingNode);

tree2 = new JTree(Treeroot);
add(tree2);


//ADD TO CALCULATION

WeghtOfSubTrees.put(tree2, calculated value));

}

//find maximum value with tree-using WeightOfSubTrees HashMap
I take selected Jtree name as a SELECTEDTREE
-------------------------------------------------------------------------
//THEN I WANT TO REPEAT. BUT WANT TO TAKE SELECTED TREE TO ABOVE PROCEDURE. IT TOOK FINAL VALUE OF HASHmAP.

tree2.removeAll(); -------------------------------???????
tree2 =SELECTEDTREE;-----------------------???????
-------------------------------------------------------------------------
WeghtOfSubTrees.clear();

}

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("New tree");
this.pack();
this.setVisible(true);

}
}

我在上面的代码中包含了编辑点。可能代码会很复杂。我想我想将新选择的 Jtree 分配给第一个 Jtree。我使用了以下代码部分。

                    tree2.removeAll(); 
tree2 =SELECTEDTREE;

但是,它不起作用。如何在 gotofor 循环 X 之前为第一个创建的树分配新树?

最佳答案

根据您共享的代码,您将节点添加到现有根并基于同一根创建树。我已经创建了克隆树的方法。

    private JTree createTree(DefaultMutableTreeNode rootNode ) {
NodeInfor3 info =(NodeInfor3)rootNode.getUserObject();
//NodeInfor3 node = new NodeInfor3(info.name, info.getSpecVal());
DefaultMutableTreeNode newRoot = new DefaultMutableTreeNode(info);
JTree tree = new JTree(newRoot);
copyChildren(rootNode, newRoot);
return tree;
}

private void copyChildren(DefaultMutableTreeNode source , DefaultMutableTreeNode target) {

for (int i = 0; i < source.getChildCount(); i++) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)source.getChildAt(i);
NodeInfor3 info =(NodeInfor3)node.getUserObject();
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(info);
target.add(newNode);
if(node.isLeaf() == false){
copyChildren(node, newNode);
}
}
}

private void refreshNodeReg(DefaultMutableTreeNode root) {
NodeInfor3 node = (NodeInfor3)root.getUserObject();
nodeReg.put(node.getName(), root);
for (int i = 0; i < root.getChildCount(); i++) {
refreshNodeReg((DefaultMutableTreeNode)root.getChildAt(i));
}
}

关于java - Java 中的两个 JTree 相等/赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37922864/

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