JTree树形结构

来源:互联网 发布:淘宝评价置顶规则 编辑:程序博客网 时间:2024/05/22 07:26

 //Tree.java
//树形结构
//2009-11-3
//<applet code=Tree width=200 height=100>
//</applet>

import javax.swing.*;
import java.awt.*;
import javax.swing.tree.*;

public class Tree extends JApplet
{
 public void init(){
  DefaultMutableTreeNode root=new DefaultMutableTreeNode("JTree");
  DefaultMutableTreeNode colors=new DefaultMutableTreeNode("Color");
  DefaultMutableTreeNode style=new DefaultMutableTreeNode("style");
  DefaultMutableTreeNode sports=new DefaultMutableTreeNode("sports");
  root.add(colors);
  root.add(style);
  root.add(sports);

  DefaultMutableTreeNode red=new DefaultMutableTreeNode("Red");
  DefaultMutableTreeNode blue=new DefaultMutableTreeNode("blue");
  DefaultMutableTreeNode white=new DefaultMutableTreeNode("white");
  colors.add(red);
  colors.add(blue);
  colors.add(white);

  DefaultMutableTreeNode liNing=new DefaultMutableTreeNode("LiNING");
  DefaultMutableTreeNode nike=new DefaultMutableTreeNode("Nike");
  sports.add(liNing);
  sports.add(nike);
  
  JTree tree=new JTree(root);
  
  Container cp=getContentPane();
  cp.setLayout(new FlowLayout());
  cp.add(tree);
 }
}

 

   

原创粉丝点击