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);
}
}
怎么给叶节点设置监听,希望有代码 展开
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);
}
}
怎么给叶节点设置监听,希望有代码 展开
展开全部
给叶节点设置监听的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());
}
}
}
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());
}
}
}
因为字数限制不能给你整个程序,你按照上面的改一下吧.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询