....设置节点的id为A、B.. 每个节点都带一个onclick事件,例如clickevent(id) function clickevent(id){ alert("你选中了"+id) }
LZ说的是swing把 = =
我之前写的一个程序涉及了这部分功能的实现,我节选一部分代码给你看一下:tree.addTreeSelectionListener(this);//Add TreeSelectionEvent to the tree /** * Add TreeSelectionEvent to tree when it is clicked * @param tse */ public void valueChanged(TreeSelectionEvent tse) { if(tse.getSource()==this){ //Get the component you select and test whether it's a leaf DefaultMutableTreeNode dmtn= (DefaultMutableTreeNode)this.getLastSelectedPathComponent(); if(dmtn.isLeaf()){ //如果选中的是树叶(结点) System.out.println("你选中了" + dmtn.toString()); } } }希望对你有用!
每个节点都带一个onclick事件,例如clickevent(id)
function clickevent(id){
alert("你选中了"+id)
}
/**
* Add TreeSelectionEvent to tree when it is clicked
* @param tse
*/
public void valueChanged(TreeSelectionEvent tse) {
if(tse.getSource()==this){
//Get the component you select and test whether it's a leaf
DefaultMutableTreeNode dmtn=
(DefaultMutableTreeNode)this.getLastSelectedPathComponent();
if(dmtn.isLeaf()){ //如果选中的是树叶(结点)
System.out.println("你选中了" + dmtn.toString());
}
}
}希望对你有用!