JTree单击事件
Posted 且穷且独立
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JTree单击事件相关的知识,希望对你有一定的参考价值。
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(); } }
以上是关于JTree单击事件的主要内容,如果未能解决你的问题,请参考以下文章