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; /* JTree的构造函数: JTree() JTree(Hashtable value) JTree(Object[] value)//只有这个构造函数可以创建多个根结点 JTree(TreeModel newModel) JTree(TreeNode root) JTree(TreeNode root, boolean asksAllowsChildren) JTree(Vector value) */ 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(); horse.isRoot(); //构造函数: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"); root.add (child1); child1.add (child11); child11.add (child111); 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(); } }在实际开发过程中会经常使用JTree组件,平时会遇到这样或那样的问题,在此将偶得一点经验写下来,与大家共享,希望对大家有所帮助。 private JTree jtNetDevice;//数组件申明 private JScrollPane jspTree;//滚动面板申明 1、初始化 DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root"); jtNetDevice = new JTree(rootNode); jtNetDevice.setAutoscrolls(true); getTreeSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);//设置单选模式 jspTree = new JScrollPane(); jspTree.getViewport().add(jtNetDevice, null); 2、三个经常使用的取值函数 private DefaultTreeModel getTreeModel(){ return (DefaultTreeModel)jtNetDevice.getModel(); } private DefaultMutableTreeNode getRootNode(){ return (DefaultMutableTreeNode)getTreeModel().getRoot(); } private TreeSelectionModel getTreeSelectionModel(){ return jtNetDevice.getSelectionModel(); } 3、根据node得到path: TreePath visiblePath = new TreePath(getTreeModel().getPathToRoot(node)); 4、根据Path展开到该节点 jtNetDevice.makeVisible(visiblePath); 5、根据path设定该节点选定 jtNetDevice.setSelectionPath(visiblePath); 6、选中节点的方法 首先,根据节点得到树路径,其中chosen为需要选中的节点 TreePath visiblePath = new TreePath( ( (DefaultTreeModel) jtNetDevice.getModel()). getPathToRoot(chosen)); 然后根据Path选中该节点 jtNetDevice.setSelectionPath(visiblePath); 7、滚动到可见位置 jtNetDevice.scrollPathToVisible(visiblePath); 8、给JTree添加右键弹出菜单 void jtNetDevice_mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) { jPopupMenu1.show(e.getComponent(), e.getX(), e.getY());//弹出右键菜单 } } 9、关于JTree的展开 // If expand is true, expands all nodes in the tree. // Otherwise, collapses all nodes in the tree. public void expandAll(JTree tree, boolean expand) { TreeNode root = (TreeNode)tree.getModel().getRoot(); // Traverse tree from root expandAll(tree, new TreePath(root), expand); } private void expandAll(JTree tree, TreePath parent, boolean expand) { // Traverse children TreeNode node = (TreeNode)parent.getLastPathComponent(); if (node.getChildCount() >= 0) { for (Enumeration e=node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode)e.nextElement(); TreePath path = parent.pathByAddingChild(n); expandAll(tree, path, expand); } } // Expansion or collapse must be done bottom-up if (expand) { tree.expandPath(parent); } else { tree.collapsePath(parent); } } 10、如何遍历JTree // 创建树 JTree tree = new JTree(); // 添加树节点...... // 遍历所有节点 visitAllNodes(tree); // 仅遍历展开的节点 visitAllExpandedNodes(tree); // Traverse all nodes in tree public void visitAllNodes(JTree tree) { TreeNode root = (TreeNode)tree.getModel().getRoot(); visitAllNodes(root); } public void visitAllNodes(TreeNode node) { // node is visited exactly once process(node); if (node.getChildCount() >= 0) { for (Enumeration e=node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode)e.nextElement(); visitAllNodes(n); } } } // Traverse all expanded nodes in tree public void visitAllExpandedNodes(JTree tree) { TreeNode root = (TreeNode)tree.getModel().getRoot(); visitAllExpandedNodes(tree, new TreePath(root)); } public void visitAllExpandedNodes(JTree tree, TreePath parent) { // Return if node is not expanded if (!tree.isVisible(parent)) { return; } // node is visible and is visited exactly once TreeNode node = (TreeNode)parent.getLastPathComponent(); process(node); // Visit all children if (node.getChildCount() >= 0) { for (Enumeration e=node.children(); e.hasMoreElements(); ) { TreeNode n = (TreeNode)e.nextElement(); TreePath path = parent.pathByAddingChild(n); visitAllExpandedNodes(tree, path); } } }
jTree
已经有近一个半月没有写总结了,蛮惭愧的,这说明我的毅力不足。一旦把精力放在专业课上,数天不敲编码,整个人就松懈下来了。前几天,参阅QQ界面相关资料时,看到了这样一句话:如今我来了,你们看到的也将不同.....而我们呢?我们来到了蓝杰,可是我们究竟改变了多少了?我想这和我们投入的时间和精力是成正比的。现在的我还很烂,不过我也会一步步让你们看到我的改变。。。。
步入正题!
这几天一直在尝试做QQ界面,查阅资料下,掌握了对JTree添加图片的方法,以及Swing组件的setBounds(int x, int y, int width, int height)方法的应用
JTree添加图片
1.首先定义根节点和叶节点
DefaultMutableTreeNode root = new DefaultMutableTreeNode("根节点"); DefaultMutableTreeNode leafNode1 = new DefaultMutableTreeNode("叶节点1");
DefaultMutableTreeNode leafNode2 = new DefaultMutableTreeNode("叶节点2");
root.add(leafNode1);
root.add(leafNode2);
2.设置树的显示风格
UIManager.getDefaults().put("Tree.lineTypeDashed", true);// 设置连接线为虚线
UIManager.getDefaults().put("Tree.lineTypeDashed",false); //设置连接线为直线
JTree tree = new JTree(root);//建树
tree.putClientProperty("JTree.lineStyle","None");//去掉连接线 tree.putClientProperty("JTree.lineStyle","Horizontal");//将树设为水平分隔风格 tree.putClientProperty("JTree.lineStyle","Angled");//默认
3.给树上节点添加图片
首先要获得树的DefaultTreeCellRenderer
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
Icon rootIcon = new ImageIcon("rootIcon路径"); Icon leafIcon = new ImageIcon("leafIcon路径");
添加图片
rend.setLeafIcon(leafIcon);//叶节点图片 rend.setOpenIcon(rootIcon);//树打开时显示的图片 rend.setClosedIcon(collIcon);//树关闭时显示的图片
若要隐藏根节点
tree.setRootVisible(false);
setBounds(int x, int y, int width, int height)方法的应用
很早以前就看到了这个方法,其说明:(x,y)为组件显示的坐标,(width,height)为组件的宽和高
但无论我怎么设置都没有用,知道前天看到了一个具体实例才豁然开朗,只需要加上一行代码:
this.setLayout(null);
郁闷,很多时候困扰我的都是一行代码的事。
思考
初学JAVA不过2个多月,我们对API类的运用还处于低级水平。我想,同小组的很多人都没有用过API类库,上面有详细的注解和实例,我也是前天才下了一个。API类库是一个很好的工具。
其次,我们添加组件调用的都是API库内现成的类,很多时候都达不到我们想要的效果,如果我们写一个类继承某个组件,在其内写方法进行设置,可能就方便很多。比如给JButton添加图标,写一个类MyJButton extends JButton ,在其构造器内传入图标。。。对JTree添图标也可以采用相同的做法。。。
这只是我的一些小想法,还没有真的用过,只是一种思路罢了。