java中Tree怎么给子结点设置监听

packageTree.java;importjava.awt.Container;importjavax.swing.JFrame;importjavax.swing.... package Tree.java;

import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeWin extends JFrame{

TreeWin()
{
setSize(300,250);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("根结点");
DefaultMutableTreeNode t1 = new DefaultMutableTreeNode("结点");
DefaultMutableTreeNode t2 = new DefaultMutableTreeNode("结点");
DefaultMutableTreeNode t1_1 = new DefaultMutableTreeNode("叶结点");
DefaultMutableTreeNode t1_2 = new DefaultMutableTreeNode("叶结点");
root.add(t1);
root.add(t2);
t1.add(t1_1);
t2.add(t1_2);
JTree tree = new JTree(root); //创建根为root的树
JScrollPane scrollPane = new JScrollPane(tree);
con.add(scrollPane);

}
public static void main(String[] args) {
TreeWin win = new TreeWin();
win.setVisible(true);
}

}

怎么给叶节点设置监听,希望有代码
展开
 我来答
紫薇参星
科技发烧友

推荐于2017-11-26 · 有一些普通的科技小锦囊
知道大有可为答主
回答量:5983
采纳率:92%
帮助的人:3617万
展开全部
给叶节点设置监听的Java程序如下:
package Tree.java;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
public class TreeWin extends JFrame implements TreeSelectionListener{
JTree tree;
TreeWin()
{
setSize(300,250);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container con = getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("根结点");
DefaultMutableTreeNode t1 = new DefaultMutableTreeNode("结点");
DefaultMutableTreeNode t2 = new DefaultMutableTreeNode("结点");
DefaultMutableTreeNode t1_1 = new DefaultMutableTreeNode("叶结点");
DefaultMutableTreeNode t1_2 = new DefaultMutableTreeNode("叶结点");
root.add(t1);
root.add(t2);
t1.add(t1_1);
t2.add(t1_2);
tree= new JTree(root); //创建根为root的树
tree.addTreeSelectionListener(this);//给整个树加选择监听
JScrollPane scrollPane = new JScrollPane(tree);
con.add(scrollPane);
}
public static void main(String[] args) {
TreeWin win = new TreeWin();
win.setVisible(true);
}
public void valueChanged(TreeSelectionEvent tse) {
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(selectionNode.isLeaf()){ //叶子节点的监听
System.out.println(selectionNode.toString());
}
}
}
追问
valueChanged()能给叶结点设置双击吗?
追答
不行,设置双击要监听鼠标事件,用mouseClicked函数,
一.把public class TreeWin extends JFrame implements TreeSelectionListener{改为
public class TreeWin extends JFrame implements MouseListener{
二.把tree.addTreeSelectionListener(this);改为tree.addMouseListener(this);
三.去掉valueChanged函数,在mouseClicked函数中写
public void mouseClicked(MouseEvent me) {
DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if(me.getClickCount()==2){ //鼠标双击事件
if(selectionNode.isLeaf()){ //叶子节点的监听
System.out.println(selectionNode.toString());
}
}
}
因为字数限制不能给你整个程序,你按照上面的改一下吧.
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式