1 import javax.swing.JFrame; 2 import javax.swing.JTree; 3 import javax.swing.event.TreeSelectionEvent; 4 import javax.swing.event.TreeSelectionListener; 5 import javax.swing.tree.DefaultMutableTreeNode; 6 7 import org.eclipse.swt.widgets.Tree; 8 9 /** 10 * 树状结构 11 * 12 * @author 404 13 */ 14 public class jtree extends JFrame { 15 16 public jtree() { 17 18 //先实例化的是父节点,实例化完父节点之后,再实例化子节点 19 DefaultMutableTreeNode Goodfriend = new DefaultMutableTreeNode("我的好友"); 20 Goodfriend.add(new DefaultMutableTreeNode("好友1")); 21 Goodfriend.add(new DefaultMutableTreeNode("好友2")); 22 Goodfriend.add(new DefaultMutableTreeNode("好友3")); 23 24 DefaultMutableTreeNode Webfriend = new DefaultMutableTreeNode("我的网友"); 25 Webfriend.add(new DefaultMutableTreeNode("网友1")); 26 Webfriend.add(new DefaultMutableTreeNode("网友2")); 27 Webfriend.add(new DefaultMutableTreeNode("网友3")); 28 DefaultMutableTreeNode Doub = new DefaultMutableTreeNode("逗逼"); 29 //用Doub这个对象add,那么逗逼就是最高级了。 30 Doub.add(Goodfriend); 31 Doub.add(Webfriend); 32 JTree jTree = new JTree(Doub); 33 this.add(jTree); 34 this.setTitle("JTree控件演示"); 35 this.setSize(200, 300); 36 this.setVisible(true); 37 this.setLocationRelativeTo(null);//居中 38 } 39 public static void main(String[] args) { 40 new jtree(); 41 } 42 }
运行结果: