随笔-3  评论-1  文章-13  trackbacks-0

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

public class Tree extends JFrame implements TreeSelectionListener {
 JTree tree=null;
 JTextArea text;
 Container con;
 JScrollPane scrollpan;
 JSplitPane splitpan;
 
 public Tree(){
  con=this.getContentPane();
  DefaultMutableTreeNode root=new DefaultMutableTreeNode("classmate");
  DefaultMutableTreeNode t1=new DefaultMutableTreeNode("sd");
  DefaultMutableTreeNode t2=new DefaultMutableTreeNode("nm");
  DefaultMutableTreeNode t1_1=new DefaultMutableTreeNode("gxc");
  DefaultMutableTreeNode  t1_2=new DefaultMutableTreeNode("jzl");
  DefaultMutableTreeNode t2_1=new DefaultMutableTreeNode("xmx");
  root.add(t1);
  root.add(t2);
  t1.add(t1_1);
  t1.add(t1_2);
  t2.add(t2_1);
  tree=new JTree(root);
  text=new JTextArea(20,20);
  scrollpan=new JScrollPane(text);
  splitpan=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,tree,scrollpan);
  tree.addTreeSelectionListener(this);
  con.add(splitpan);
  setVisible(true);
  this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  
  
 }

 public void valueChanged(TreeSelectionEvent e) {
  // TODO 自动生成方法存根
  if(e.getSource()==tree){
   DefaultMutableTreeNode node =(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
   if(node.isLeaf()){
    String s=node.toString();
     if(s.equals("gxc")){
       text.setText("sd");
     }else if(s.equals("xmx")){
      text.setText("nm");
     }else{
      text.setText(node.getUserObject().toString());
     }
   }
  }
 }
 public static void main(String para[]){
  Tree tree=new Tree();
 }

}

posted on 2007-07-16 12:44 lcgxc 阅读(275) 评论(0)  编辑  收藏 所属分类: java

只有注册用户登录后才能发表评论。


网站导航: