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

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(); 
    } 
 

 


摘自 Gaowen_HAN的专栏

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