关与JTree的个问题!请高手帮忙!先谢谢了哈。
为什么用JTree生成的数中单击子节点不会响应事件?
我的程序如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
class Mtree extends JFrame implements TreeSelectionListener
{ JTree tree=null;
JPanel p=null;
JButton button;
Mtree(String s)
{ super(s);
Container con=this.getContentPane();
DefaultMutableTreeNode root=new DefaultMutableTreeNode( "主业务功能 ");
DefaultMutableTreeNode t1=new DefaultMutableTreeNode( "账户管理 ");
DefaultMutableTreeNode t2=new DefaultMutableTreeNode( "事物查询 ");
DefaultMutableTreeNode t3=new DefaultMutableTreeNode( "用户存款 ");
DefaultMutableTreeNode t4=new DefaultMutableTreeNode( "用户取款 ");
DefaultMutableTreeNode t5=new DefaultMutableTreeNode( "数据维护 ");
DefaultMutableTreeNode t6=new DefaultMutableTreeNode( "系统表查看 ");
DefaultMutableTreeNode t7=new DefaultMutableTreeNode( "系统账户管理 ");
DefaultMutableTreeNode t8=new DefaultMutableTreeNode( "退出系统 ");
DefaultMutableTreeNode t1_1=new DefaultMutableTreeNode( "新用户开户 ");
DefaultMutableTreeNode t1_2=new DefaultMutableTreeNode( "用户资料明细 ");
DefaultMutableTreeNode t1_3=new DefaultMutableTreeNode( "账户密码修改 ");
DefaultMutableTreeNode t2_1=new DefaultMutableTreeNode( "账户金额查询 ");
DefaultMutableTreeNode t2_2=new DefaultMutableTreeNode( "交易记录查询 ");
DefaultMutableTreeNode t2_3=new DefaultMutableTreeNode( "账户信息查询 ");
DefaultMutableTreeNode t5_1=new DefaultMutableTreeNode( "数据导出 ");
DefaultMutableTreeNode t5_2=new DefaultMutableTreeNode( "数据导入 ");
DefaultMutableTreeNode t7_1=new DefaultMutableTreeNode( "添加管理员账户 ");
DefaultMutableTreeNode t7_2=new DefaultMutableTreeNode( "管理员密码修改 ");
root.add(t1);root.add(t2);root.add(t3);root.add(t4);
root.add(t5);root.add(t6);root.add(t7);root.add(t8);
t1.add(t1_1);t1.add(t1_2);t1.add(t1_3);
t2.add(t2_1);t2.add(t2_2);t2.add(t2_3);
t5.add(t5_1);t5.add(t5_2);t7.add(t7_1);t7.add(t7_2);
JTree tree= new JTree(root);
JScrollPane panel = new JScrollPane(p=new JPanel());
JSplitPane splitpane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,tree,panel);
con.add(splitpane);
tree.addTreeSelectionListener(this);
System.out.println(tree);
this.validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.setBounds(80, 60, 800, 600);
this.setVisible(true);
System.out.println( "333 ");
}
public void valueChanged(TreeSelectionEvent e)
{ // TODO 自动生成方法存根
System.out.println( "444 ");
if(e.getSource()==tree)//这里似乎无法获得事件源