JTree组件是另外一个复杂组件,它不像 JTable 那样难用,但是也不像 JList 那么容易。使用 JTree 时麻烦的部分是它要求的数据模型。
JTree组件的功能来自树的概念,树有分支和叶子。您在 Windows 中使用 IE 浏览器时,可能非常熟悉这个概念 —— 可以展开和折叠分支,显示可以选择和取消选择的不同叶子。
您很有可能发现树在应用程序中不像表格或列表那样有用,所以在 Internet 上没有许多有帮助的这方面的示例。实际上,像 JTable 一样,JTree 没有什么入门级的功能。如果决定使用 JTree,那么立即就可以达到中级水平,当然还必须学习随之而来的概念。因此,示例应用程序没有介绍 JTree,所以也很不幸,不管是入门教程还是中级教程,都没有涉及这个不太流行的组件。
但是,树有一些时候是符合需求的合理的 UI 组件。文件/目录系统就是一个示例(就像在 IE 浏览器中那样),而且当数据采取层次结构的时候,也就是说数据采用树的形式的时候,JTree组件就是***组件。
在 Swing 中,JTree组件看起来如下所示:
- import java.awt.Dimension;
- import java.awt.Color;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTree;
- import javax.swing.BoxLayout;
- import javax.swing.tree.TreePath;
- import javax.swing.tree.DefaultMutableTreeNode;
- import javax.swing.tree.DefaultTreeModel;
- public class JTreeDemo
- {
- public static void main (String[] args)
- {
- //构造函数:JTree()
- JTree example1 = new JTree();
- //构造函数:JTree(Object[] value)
- Object[] letters= {"a", "b", "c", "d", "e"};
- JTree example2 = new JTree (letters);
- //构造函数:JTree(TreeNode root)(TreeNode空)
- //用空结点创建树
- DefaultMutableTreeNode node1 = new DefaultMutableTreeNode();
- //定义树结点
- JTree example3 = new JTree (node1);
- //用此树结点做参数调用 JTree的构造函数创建含有一个根结点的树
- //构造函数:JTree(TreeNode root)(同上,只是TreeNode非空)
- //用一个根结点创建树
- DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("Color");
- JTree example4 = new JTree (node2);//结点不可以颜色,默认为白面黑字
- example4.setBackground (Color.lightGray);
- //构造函数:JTree(TreeNode root, boolean asksAllowsChildren)
(同上,只是TreeNode又有不同)- //使用DefaultMutableTreeNode类先用一个根结点创建树,
设置为可添加孩子结点,再添加孩子结点- DefaultMutableTreeNode color = new DefaultMutableTreeNode
("Color", true);- DefaultMutableTreeNode gray = new DefaultMutableTreeNode
("Gray");- color.add (gray);
- color.add (new DefaultMutableTreeNode ("Red"));
- gray.add (new DefaultMutableTreeNode ("Lightgray"));
- gray.add (new DefaultMutableTreeNode ("Darkgray"));
- color.add (new DefaultMutableTreeNode ("Green"));
- JTree example5 = new JTree (color);
- //构造函数:JTree(TreeNode root)(同上,只是TreeNode非空)
- //通过逐个添加结点创建树
- DefaultMutableTreeNode biology = new DefaultMutableTreeNode ("Biology");
- DefaultMutableTreeNode animal = new DefaultMutableTreeNode ("Animal");
- DefaultMutableTreeNode mammal = new DefaultMutableTreeNode ("Mammal");
- DefaultMutableTreeNode horse = new DefaultMutableTreeNode ("Horse");
- mammal.add (horse);
- animal.add (mammal);
- biology.add (animal);
- JTree example6 = new JTree (biology);
- horse.isLeaf();//if this node allows children.
- horse.isRoot();//true if this node is the root of its tree
- //构造函数:JTree(TreeModel newModel)
- //用DefaultMutableTreeNodel类定义一个结点再用这个结点做参数定义一个用DefaultTreeMode
- //创建一个树的模型,再用JTree的构造函数创建一个树
- DefaultMutableTreeNode root = new DefaultMutableTreeNode ("Root1");
- DefaultMutableTreeNode child1 = new DefaultMutableTreeNode ("Child1");
- DefaultMutableTreeNode child11 = new DefaultMutableTreeNode ("Child11");
- DefaultMutableTreeNode child111 = new DefaultMutableTreeNode ("Child111");
- DefaultMutableTreeNode child1111 = new DefaultMutableTreeNode ("Child1111");
- root.add (child1); child1.add (child11);
- child11.add (child111);child111.add(child1111);
- DefaultTreeModel model = new DefaultTreeModel (root);
- JTree example7 = new JTree (model);
- JPanel panel = new JPanel();
- panel.setLayout (new BoxLayout (panel, BoxLayout.X_AXIS));
- panel.setPreferredSize (new Dimension (700, 400));
- panel.add (new JScrollPane (example1));//JTree必须放在JScrollPane上
- panel.add (new JScrollPane (example2));
- panel.add (new JScrollPane (example3));
- panel.add (new JScrollPane (example4));
- panel.add (new JScrollPane (example5));
- panel.add (new JScrollPane (example6));
- panel.add (new JScrollPane (example7));
- JFrame frame = new JFrame ("JTreeDemo");
- frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
- frame.setContentPane (panel);
- frame.pack();
- frame.show();
- }
- }
【编辑推荐】