这是一个树形结构数据,是从jsp页面点击某个节点下的得到的树结构,
ID yid name
562 26 a
563 562 b
565 563 c
569 565 d
574 569 f7
576 569 f8
570 565 d1
566 563 c1
567 563 c2
571 567 C001
568 563 c3
572 568 f3
573 568 f4
564 562 f5
我想根据这个结构,在复制生成一个新的树形跟上面一样的树的结构,比如id自动累加,yid的第一个id是根据复制那个节点的ID.才能根据第一个结构生成这样的结果
ID yid name
1 0 a
2 1 b
3 2 c
4 3 d
5 4 f7
6 4 f8
7 3 d1
8 2 c1
9 2 c2
10 9 C001
11 2 c3
12 11 f3
13 11 f4
14 1 f5
万分感谢!最好贴代码,这个能很长时间,能不出来,高手救命
这个树就差复制这个功能,主要是为了跟数据库交互,要把复制的完的树形结构存到数据库里,高手们,帮帮忙,郁闷很长时间,就是不知道yid怎么生成,万分感谢
高手快来,救命呀,java如何复制树,一定要帮帮忙,郁闷很长时间了
答案:2 悬赏:10 手机版
解决时间 2021-02-26 19:21
- 提问者网友:星軌
- 2021-02-26 12:37
最佳答案
- 五星知识达人网友:何以畏孤独
- 2021-02-26 13:49
给你一个简单实例:
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.BorderLayout;
public class TreeDemo {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String[] strings = {"aaa","bbb","ccc"};
DefaultMutableTreeNode rooTreeNode;
DefaultMutableTreeNode sub1;
DefaultMutableTreeNode sub2;
DefaultMutableTreeNode sub3;
public TreeDemo()
{
sub1 = new DefaultMutableTreeNode("A");
sub2 = new DefaultMutableTreeNode("B");
sub3 = new DefaultMutableTreeNode("C");
for(int i=0;i
{
sub1.add(new DefaultMutableTreeNode(strings[i]));
sub2.add(new DefaultMutableTreeNode(strings[i]));
sub3.add(new DefaultMutableTreeNode(strings[i]));
}
rooTreeNode = new DefaultMutableTreeNode("ROOT");
rooTreeNode.add(sub1);
rooTreeNode.add(sub2);
rooTreeNode.add(sub3);
JTree tree = new JTree(rooTreeNode);
JScrollPane scrollPane = new JScrollPane(tree); //滚动,防止超出范围
panel.setLayout(new BorderLayout());
panel.add(scrollPane,BorderLayout.CENTER);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
new TreeDemo();
}
}
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.BorderLayout;
public class TreeDemo {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
String[] strings = {"aaa","bbb","ccc"};
DefaultMutableTreeNode rooTreeNode;
DefaultMutableTreeNode sub1;
DefaultMutableTreeNode sub2;
DefaultMutableTreeNode sub3;
public TreeDemo()
{
sub1 = new DefaultMutableTreeNode("A");
sub2 = new DefaultMutableTreeNode("B");
sub3 = new DefaultMutableTreeNode("C");
for(int i=0;i
sub1.add(new DefaultMutableTreeNode(strings[i]));
sub2.add(new DefaultMutableTreeNode(strings[i]));
sub3.add(new DefaultMutableTreeNode(strings[i]));
}
rooTreeNode = new DefaultMutableTreeNode("ROOT");
rooTreeNode.add(sub1);
rooTreeNode.add(sub2);
rooTreeNode.add(sub3);
JTree tree = new JTree(rooTreeNode);
JScrollPane scrollPane = new JScrollPane(tree); //滚动,防止超出范围
panel.setLayout(new BorderLayout());
panel.add(scrollPane,BorderLayout.CENTER);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
public static void main(String[] args) {
new TreeDemo();
}
}
全部回答
- 1楼网友:封刀令
- 2021-02-26 14:44
没看懂什么意思?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯