import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class MyFrame extends JFrame {
private JTree tree;
public MyFrame() {
setTree();
this.add(tree);
this.setSize(500, 300);
}
public void setTree() {
DefaultMutableTreeNode lx=new DefaultMutableTreeNode("联系人");
DefaultMutableTreeNode qr=new DefaultMutableTreeNode("亲人");
DefaultMutableTreeNode b=new DefaultMutableTreeNode("爸");
DefaultMutableTreeNode j=new DefaultMutableTreeNode("姐");
DefaultMutableTreeNode tx=new DefaultMutableTreeNode("同学");
DefaultMutableTreeNode cy=new DefaultMutableTreeNode("程元");
DefaultMutableTreeNode py=new DefaultMutableTreeNode("朋友");
DefaultMutableTreeNode wl=new DefaultMutableTreeNode("A荣天");
lx.add(qr);
qr.add(b);
qr.add(j);
lx.add(tx);
tx.add(cy);
lx.add(py);
py.add(wl);
//JAVA 树形结构程序事件实例
tree = new JTree(lx);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent(); // 返回当前选择的第一个节点中的最后一个路径组件。
if(node == null) return;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class MyFrame extends JFrame {
private JTree tree;
public MyFrame() {
setTree();
this.add(tree);
this.setSize(500, 300);
}
public void setTree() {
DefaultMutableTreeNode lx=new DefaultMutableTreeNode("联系人");
DefaultMutableTreeNode qr=new DefaultMutableTreeNode("亲人");
DefaultMutableTreeNode b=new DefaultMutableTreeNode("爸");
DefaultMutableTreeNode j=new DefaultMutableTreeNode("姐");
DefaultMutableTreeNode tx=new DefaultMutableTreeNode("同学");
DefaultMutableTreeNode cy=new DefaultMutableTreeNode("程元");
DefaultMutableTreeNode py=new DefaultMutableTreeNode("朋友");
DefaultMutableTreeNode wl=new DefaultMutableTreeNode("A荣天");
lx.add(qr);
qr.add(b);
qr.add(j);
lx.add(tx);
tx.add(cy);
lx.add(py);
py.add(wl);
//JAVA 树形结构程序事件实例
tree = new JTree(lx);
tree.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent(); // 返回当前选择的第一个节点中的最后一个路径组件。
if(node == null) return;