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