Java JTree_3

/**
 * 
 */
package com.han;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

/**
 * This class is to enumerate all the nodes of a tree in different ways ...
 * 遍历树节点
 * @author HAN
 *
 */
public class JTree_3 extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4405016910342910815L;
	DefaultMutableTreeNode root;
	/**
	 * 
	 */
	public JTree_3() {
		root = new DefaultMutableTreeNode("ROOT");
		DefaultMutableTreeNode nodeA =  new DefaultMutableTreeNode("nodeA");
		DefaultMutableTreeNode nodeB =  new DefaultMutableTreeNode("nodeB");
		DefaultMutableTreeNode nodeC =  new DefaultMutableTreeNode("nodeC");
		root.add(nodeA);
		root.add(nodeB);
		root.add(nodeC);
		
		DefaultMutableTreeNode nodeAA =  new DefaultMutableTreeNode("nodeAA");
		DefaultMutableTreeNode nodeAB =  new DefaultMutableTreeNode("nodeAB");
		nodeA.add(nodeAA);
		nodeA.add(nodeAB);
		
		DefaultMutableTreeNode nodeCA =  new DefaultMutableTreeNode("nodeCA");
		DefaultMutableTreeNode nodeCB =  new DefaultMutableTreeNode("nodeCB");
		DefaultMutableTreeNode nodeCC =  new DefaultMutableTreeNode("nodeCC");
		nodeC.add(nodeCA);
		nodeC.add(nodeCB);
		nodeC.add(nodeCC);
		
		DefaultMutableTreeNode nodeCBA =  new DefaultMutableTreeNode("nodeCBA");
		DefaultMutableTreeNode nodeCBB =  new DefaultMutableTreeNode("nodeCBB");
		nodeCB.add(nodeCBA);
		nodeCB.add(nodeCBB);
		
		DefaultTreeModel defaultTreeModel = new DefaultTreeModel(root);
		JTree tree = new JTree(defaultTreeModel);
		
		@SuppressWarnings("rawtypes")
		Enumeration enumeration = root.preorderEnumeration();
		while (enumeration.hasMoreElements()) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumeration.nextElement();
			TreePath treePath = new TreePath(node.getPath());
			tree.expandPath(treePath);
		}
		
		Container container = getContentPane();
		container.add(tree, BorderLayout.WEST);
		// Dimension dimension = container.getPreferredSize();
		JPanel panel = new JPanel(new GridLayout(5, 1, 10, 10));
		JButton b1 = new JButton("按前序遍历");
		JButton b2 = new JButton("按后序遍历");
		JButton b3 = new JButton("以广度优先遍历");
		JButton b4 = new JButton("以深度优先遍历");
		JButton b5 = new JButton("遍历直属子节点");
		panel.add(b1);
		panel.add(b2);
		panel.add(b3);
		panel.add(b4);
		panel.add(b5);
		container.add(panel, BorderLayout.CENTER);
		
		b1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				enumerate("按前序遍历");
			}
			
		});
		
		b2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				enumerate("按后序遍历");
			}
			
		});
		
		b3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				enumerate("以广度优先遍历");
			}
			
		});
		
		b4.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				enumerate("以深度优先遍历");
			}
			
		});
		
		b5.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				enumerate("遍历直属子节点");
			}
			
		});
	}

	private void enumerate(String mode) {
		@SuppressWarnings("rawtypes")
		Enumeration enumeration;
		if (mode.equals("按前序遍历")) {
			enumeration = root.preorderEnumeration();
		}else if (mode.equals("按后序遍历")) {
			enumeration = root.postorderEnumeration();
		}else if (mode.equals("以广度优先遍历")) {
			enumeration = root.breadthFirstEnumeration();
		}else if (mode.equals("以深度优先遍历")) {
			enumeration = root.depthFirstEnumeration();
		}else {
			enumeration = root.children();
		}
		
		System.out.println(mode + " :");
		System.out.println("_______________________________");
		while (enumeration.hasMoreElements()) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumeration.nextElement();
			for (int i = 0; i < node.getLevel(); i++) { // 根据节点的级别输出占位符
				System.out.print("----");
			}
			System.out.println(node.getUserObject()); // 输出节点标签
		}
		System.out.println();
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JTree_3 instance = new JTree_3();
		instance.setTitle("遍历树节点");
		instance.setVisible(true);
		instance.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		instance.pack();
	}

}


posted on 2012-05-15 05:55  java课程设计例子  阅读(134)  评论(0编辑  收藏  举报