当前位置:编程学习 > JAVA >>

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("按后序遍历&qu

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,