jtree节点的单击事件 很简单 在线等 速度答的有加分
建立一个jtree(root)root.add(father);father.add(child);建立以个jTextField;当鼠标左键点击child这个节点的时候,...
建立一个jtree(root)
root.add(father);
father.add(child);
建立以个jTextField;
当鼠标左键点击child这个节点的时候,jTextField.setText("I LOVE YOU"); 展开
root.add(father);
father.add(child);
建立以个jTextField;
当鼠标左键点击child这个节点的时候,jTextField.setText("I LOVE YOU"); 展开
1个回答
展开全部
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.FlowLayout;
import java.awt.GridLayout;
public class TreeDemo extends JFrame{
JTree jTree=new JTree();
JTextField jTextField =new JTextField();
GridLayout lay=new GridLayout(1,1);
public TreeDemo(){
treeinit();
this.setSize(400,400);
this.setLayout(lay);
this.add(jTree);
this.add(jTextField);
this.setVisible(true);
jTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
jTreeValueChanged(evt);
}
});
}
private void jTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();//返回最后选定的节点
if(selectedNode.toString().equals("child")){
jTextField.setText("I LOVE YOU");
}
}
public void treeinit(){
DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
DefaultMutableTreeNode father=new DefaultMutableTreeNode("father");
DefaultMutableTreeNode child=new DefaultMutableTreeNode("child");
father.add(child);
root.add(father);
TreeModel treeModel = new DefaultTreeModel(root);
jTree.setModel(treeModel);
}
public static void main(String args[]){
new TreeDemo();
}
}
import javax.swing.tree.*;
import java.awt.FlowLayout;
import java.awt.GridLayout;
public class TreeDemo extends JFrame{
JTree jTree=new JTree();
JTextField jTextField =new JTextField();
GridLayout lay=new GridLayout(1,1);
public TreeDemo(){
treeinit();
this.setSize(400,400);
this.setLayout(lay);
this.add(jTree);
this.add(jTextField);
this.setVisible(true);
jTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
jTreeValueChanged(evt);
}
});
}
private void jTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {
DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();//返回最后选定的节点
if(selectedNode.toString().equals("child")){
jTextField.setText("I LOVE YOU");
}
}
public void treeinit(){
DefaultMutableTreeNode root=new DefaultMutableTreeNode("root");
DefaultMutableTreeNode father=new DefaultMutableTreeNode("father");
DefaultMutableTreeNode child=new DefaultMutableTreeNode("child");
father.add(child);
root.add(father);
TreeModel treeModel = new DefaultTreeModel(root);
jTree.setModel(treeModel);
}
public static void main(String args[]){
new TreeDemo();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询