import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; import java.awt.*; import java.io.File; public class TreeDemo extends JFrame { JTree tree; public TreeDemo(){ super("A tree"); setSize(400,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); createtree(); add(new JScrollPane(tree)); setVisible(true); } public void createtree() { DefaultMutableTreeNode root = new DefaultMutableTreeNode(new treenode("E:\\NETBACKUP\\文档")); DefaultMutableTreeNode node1 = new DefaultMutableTreeNode(new treenode("E:\\NETBACKUP\\文档\\文档刷新")); DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(new treenode("E:\\NETBACKUP\\文档\\文档刷新\\Centralized Backup Installation Manual.doc")); DefaultMutableTreeNode node3 = new DefaultMutableTreeNode(new treenode("E:\\NETBACKUP\\文档\\文档刷新\\Centralized Backup Installation Manual.doc")); root.add(node1); //root.add(node1); node1.add(node2); node2.add(node3); tree = new JTree(root); tree.setEditable(true); } public static void main(String[] args) { new TreeDemo(); } } class treenode extends File{ public treenode(String name){ super(name); } public String toString() { return getName(); } }