码迷,mamicode.com
首页 > 编程语言 > 详细

java中树节点的应用

时间:2014-12-07 00:04:36      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   java   on   div   

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

public class JTreeDemo extends JFrame{

    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public JTreeDemo(){
        //根节点
        DefaultMutableTreeNode group =new DefaultMutableTreeNode("根节点");
        //二级节点
        DefaultMutableTreeNode first=new DefaultMutableTreeNode("二级节点1");
        DefaultMutableTreeNode second = new DefaultMutableTreeNode("二级节点2");
        DefaultMutableTreeNode thrid = new DefaultMutableTreeNode("二级节点3");
        //三级节点
        DefaultMutableTreeNode first1 = new DefaultMutableTreeNode("三级节点11");
        DefaultMutableTreeNode first2 = new DefaultMutableTreeNode("三级节点12");
        
        DefaultMutableTreeNode second1 = new DefaultMutableTreeNode("三级节点21");
        DefaultMutableTreeNode second2 = new DefaultMutableTreeNode("三级节点22");
        DefaultMutableTreeNode second3 = new DefaultMutableTreeNode("三级节点23");
        
        DefaultMutableTreeNode thrid1 = new DefaultMutableTreeNode("三级节点31");
        DefaultMutableTreeNode thrid2 = new DefaultMutableTreeNode("三级节点32");
        DefaultMutableTreeNode thrid3 = new DefaultMutableTreeNode("三级节点33");
        
        //将三级节点添加到二级节点上
        first.add(first1);
        first.add(first2);
        
        second.add(second1);
        second.add(second2);
        second.add(second3);
        
        thrid.add(thrid1);
        thrid.add(thrid2);
        thrid.add(thrid3);
        
        //将二级节点添加到一级节点上
        group.add(first);
        group.add(second);
        group.add(thrid);
        
        JTree tree = new JTree(group);        //以group为参数创建根目录
        tree.collapseRow(1);                //总是显示根目录在前
        tree.setToggleClickCount(1);        //设置鼠标单击数
        
        this.getContentPane().add(tree);
        setSize(300,300);
        setVisible(true);
        setTitle("树的应用");
    }
    
    public static void main(String[] args) {
        new JTreeDemo();
    }
}

 

java中树节点的应用

标签:style   blog   io   ar   color   sp   java   on   div   

原文地址:http://www.cnblogs.com/w-xc/p/4148968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!