JAVA:实现树结构-树组件-树目录

 
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

public class Tree组件 extends JFrame {
	private JPanel contentPane;

	public Tree组件() {
		setTitle("为树组件增加节点");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 250);
		contentPane = new JPanel();

		DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("第一目录");
		DefaultMutableTreeNode childeNode1 = new DefaultMutableTreeNode("第二目录");
		DefaultMutableTreeNode childeNode2 = new DefaultMutableTreeNode("第三目录");
		DefaultMutableTreeNode childeNode3 = new DefaultMutableTreeNode("第四目录");
		DefaultMutableTreeNode childeNode4 = new DefaultMutableTreeNode("第五目录");
		DefaultMutableTreeNode childeNode5 = new DefaultMutableTreeNode("第六目录");
		childeNode1.add(childeNode2);
		childeNode1.add(childeNode3);
		childeNode1.add(childeNode4);
		childeNode2.add(childeNode5);
		childeNode3.add(childeNode5);
		childeNode3.add(childeNode4);
		rootNode.add(childeNode1);
		rootNode.add(childeNode2);
		JTree tree = new JTree(rootNode);                //一定把所有的树枝加到树上
		scrollPane.setViewportView(tree);
	}

	public static void main(String[] args) {
		Tree组件 frame = new Tree组件();
		frame.setVisible(true);
	}
}

 

原理特别简单,可以自行修改

 

 


JAVA:实现树结构-树组件-树目录_第1张图片

 

你可能感兴趣的:(JAVA/JSP)