import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode; import org.eclipse.swt.widgets.Tree; /**
* 树状结构
*
* @author 404
*/
public class jtree extends JFrame { public jtree() { //先实例化的是父节点,实例化完父节点之后,再实例化子节点
DefaultMutableTreeNode Goodfriend = new DefaultMutableTreeNode("我的好友");
Goodfriend.add(new DefaultMutableTreeNode("好友1"));
Goodfriend.add(new DefaultMutableTreeNode("好友2"));
Goodfriend.add(new DefaultMutableTreeNode("好友3")); DefaultMutableTreeNode Webfriend = new DefaultMutableTreeNode("我的网友");
Webfriend.add(new DefaultMutableTreeNode("网友1"));
Webfriend.add(new DefaultMutableTreeNode("网友2"));
Webfriend.add(new DefaultMutableTreeNode("网友3"));
DefaultMutableTreeNode Doub = new DefaultMutableTreeNode("逗逼");
//用Doub这个对象add,那么逗逼就是*了。
Doub.add(Goodfriend);
Doub.add(Webfriend);
JTree jTree = new JTree(Doub);
this.add(jTree);
this.setTitle("JTree控件演示");
this.setSize(200, 300);
this.setVisible(true);
this.setLocationRelativeTo(null);//居中
}
public static void main(String[] args) {
new jtree();
}
}
运行结果: