高手快来,救命呀,java如何复制树,一定要帮帮忙,郁闷很长时间了

2024-11-18 01:40:03
推荐回答(1个)
回答1:

给你一个简单实例:
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();
}

}