Java JTree_1


package com.han;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class JTree_1 extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2628449366554214781L;
	public JTree_1() {
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("根节点");
		DefaultMutableTreeNode nodeFirst = new DefaultMutableTreeNode("一级子节点A");
		root.add(nodeFirst);
		DefaultMutableTreeNode nodeSecond = new DefaultMutableTreeNode("二级子节点", false);
		nodeFirst.add(nodeSecond);
		root.add(new DefaultMutableTreeNode("一级子节点B"));
		JTree treeRoot = new JTree(root);
		getContentPane().add(treeRoot, BorderLayout.WEST);
		
		DefaultTreeModel defaultTreeModel = new DefaultTreeModel(root);
		JTree treeDefault = new JTree(defaultTreeModel);
		getContentPane().add(treeDefault, BorderLayout.CENTER);
		
		// recommended creating method
		DefaultTreeModel pointedTreeModel = new DefaultTreeModel(root, true);
		JTree treePointed = new JTree(pointedTreeModel);
		getContentPane().add(treePointed, BorderLayout.EAST);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JTree_1 instance = new JTree_1();
		instance.setVisible(true);
		instance.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		instance.pack();
	}

}


你可能感兴趣的:(Java JTree_1)