JTree组件被用来以树的形式显示数据,一层套一层,看起来清晰明了,使用户很方便地就了解到各个节点之间的层次关系,可以很容易地找到相关的数据。
例如 Windows 系统的文件管理器,它就是一个典型的树层次结构。
JTree的构造函数:
·JTree() 建立一棵默认的树。
·JTree(Hashtable data) 利用Hashtable建立树。
·JTree(Object[] data) 利用数组建立树。
·JTree(TreeModel model) 利用TreeModel建立树。
·JTree(TreeNode root) 建立以root为根的树。
·JTree(TreeNode root,Boolean children) 建立以 root 为根的树,并设定是否允许有子节点。
·JTree(Vector value) 利用Vector建立树。
使用一个JTree可以简单地像下面这样表示:
import javax.swing.JFrame
import javax.swing.JTree
import javax.swing.event.TreeSelectionEvent
import javax.swing.event.TreeSelectionListener
import javax.swing.tree.DefaultMutableTreeNode
public class TreeDemo {
public static void main(String[] args) {
// 创建没有父节点和子节点、但允许有子节点的树节点,并使用指定的用户对象对它进行初始化。
// public DefaultMutableTreeNode(Object userObject)
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("软件部")
node1.add(new DefaultMutableTreeNode(new User("小花")))
node1.add(new DefaultMutableTreeNode(new User("小虎")))
node1.add(new DefaultMutableTreeNode(new User("小龙")))
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("销售部")
node2.add(new DefaultMutableTreeNode(new User("小叶")))
node2.add(new DefaultMutableTreeNode(new User("小雯")))
node2.add(new DefaultMutableTreeNode(new User("小夏")))
DefaultMutableTreeNode top = new DefaultMutableTreeNode("职员管理")
top.add(new DefaultMutableTreeNode(new User("总经理")))
top.add(node1)
top.add(node2)
final JTree tree = new JTree(top)
JFrame f = new JFrame("JTreeDemo")
f.add(tree)
f.setSize(300, 300)
f.setVisible(true)
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
// 添加选择事件
tree.addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree
.getLastSelectedPathComponent()
if (node == null)
return
Object object = node.getUserObject()
if (node.isLeaf()) {
User user = (User) object
System.out.println("你选择了:" + user.toString())
}
}
})
}
}
class User {
private String name
public User(String n) {
name = n
}
// 重点在toString,节点的显示文本就是toString
public String toString() {
return name
}
}
import javax.swing.event.TreeSelectionEventimport javax.swing.event.TreeSelectionListener
import javax.swing.tree.DefaultMutableTreeNode
import javax.swing.tree.TreePath
import javax.swing.JFrame
import java.awt.Color
public class StudentMain extends JFrame implements TreeSelectionListener,ActionListener {
private DefaultMutableTreeNode root
private DefaultMutableTreeNode nallbook
private DefaultMutableTreeNode nallbooksearch
private DefaultMutableTreeNode nusermes
private DefaultMutableTreeNode nusermessearch
private DefaultMutableTreeNode nowningfeeuser
private DefaultMutableTreeNode nowningfeeusersearch
private DefaultMutableTreeNode student_sea_tea_infor
private DefaultMutableTreeNode stu_sea_tea_infor
public StudentMain(int zhanghao){
super("欢迎使用重大成绩管理系统:")
root=new DefaultMutableTreeNode("系统")
nallbook=new DefaultMutableTreeNode("学生个人信息查询")
nusermes=new DefaultMutableTreeNode("学生个人成绩查询")
nowningfeeuser=new DefaultMutableTreeNode("学生成绩排名查询")
student_sea_tea_infor=new DefaultMutableTreeNode("查询所有任课老师信息")
root.add(nallbook)
root.add(nusermes)
root.add(nowningfeeuser)
root.add(student_sea_tea_infor)
nallbooksearch=new DefaultMutableTreeNode("学生个人信息查询")
nallbook.add(nallbooksearch)
nusermessearch=new DefaultMutableTreeNode("学生个人成绩查询")
nusermes.add(nusermessearch)
nowningfeeusersearch=new DefaultMutableTreeNode("学生成绩排名查询")
nowningfeeuser.add(nowningfeeusersearch)
stu_sea_tea_infor=new DefaultMutableTreeNode("查询所有任课老师信息")
student_sea_tea_infor.add(stu_sea_tea_infor)
tree=new JTree(root)
tree.setRowHeight(24)
tree.putClientProperty("JTree.LineStyle", "Angled")
tree.setRootVisible(true)
tree.addTreeSelectionListener(this)
〉右击其中一个节点d出另一个窗口jTree.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e ) {
if( SwingUtilities.isRightMouseButton (e) )
if( jTree.getPathForLocation(e.getX(), e.getY()) != null)
jWindow.setVisible(true)
}
})
〉JTree树可以对象化,但节点并不是对象
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)