java something

          不要以為......很遙遠
          隨筆 - 23, 文章 - 1, 評論 - 2, 引用 - 0
          數(shù)據(jù)加載中……

          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;
          /*
          JTree的構造函數(shù):
          JTree()
          JTree(Hashtable value)
          JTree(Object[] value)//只有這個構造函數(shù)可以創(chuàng)建多個根結點
          JTree(TreeModel newModel)
          JTree(TreeNode root)
          JTree(TreeNode root, boolean asksAllowsChildren)
          JTree(Vector value)

          */
          public   class  JTreeDemo
          {
           
          public   static   void  main (String[] args)
           {


            
          // 構造函數(shù):JTree()
            JTree example1  =   new  JTree();

           

            
            
          // 構造函數(shù):JTree(Object[] value)
            Object[] letters =  { " a " " b " " c " " d " " e " };
            JTree example2 
          =   new  JTree (letters);

           


            
          // 構造函數(shù):JTree(TreeNode root)(TreeNode空)
            
          // 用空結點創(chuàng)建樹
            DefaultMutableTreeNode node1  =   new  DefaultMutableTreeNode(); // 定義樹結點
            JTree example3  =   new  JTree (node1); // 用此樹結點做參數(shù)調(diào)用 JTree的構造函數(shù)創(chuàng)建含有一個根結點的樹

           


            
          // 構造函數(shù):JTree(TreeNode root)(同上,只是TreeNode非空)
            
          // 用一個根結點創(chuàng)建樹
            DefaultMutableTreeNode node2  =   new  DefaultMutableTreeNode( " Color " );
            JTree example4 
          =   new  JTree (node2); // 結點不可以顏色,默認為白面黑字
            example4.setBackground (Color.lightGray);

           


            
          // 構造函數(shù):JTree(TreeNode root, boolean asksAllowsChildren)(同上,只是TreeNode又有不同)
            
          // 使用DefaultMutableTreeNode類先用一個根結點創(chuàng)建樹,設置為可添加孩子結點,再添加孩子結點
            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);
            
            
            
            
            
          // 構造函數(shù):JTree(TreeNode root)(同上,只是TreeNode非空)
            
          // 通過逐個添加結點創(chuàng)建樹
            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();
            
            


            
          // 構造函數(shù):JTree(TreeModel newModel)
            
          // 用DefaultMutableTreeNodel類定義一個結點再用這個結點做參數(shù)定義一個用DefaultTreeMode
            
          // 創(chuàng)建一個樹的模型,再用JTree的構造函數(shù)創(chuàng)建一個樹
            
            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();
           }
          }
          ××××××××××××××××××××××××××××××××××××××××××××××

          在實際開發(fā)過程中會經(jīng)常使用JTree組件,平時會遇到這樣或那樣的問題,在此將偶得一點經(jīng)驗寫下來,與大家共享,希望對大家有所幫助。

          private JTree jtNetDevice;//數(shù)組件申明
          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、三個經(jīng)常使用的取值函數(shù)
            private DefaultTreeModel getTreeModel(){
              return (DefaultTreeModel)jtNetDevice.getModel();
            }

            private DefaultMutableTreeNode getRootNode(){
              return (DefaultMutableTreeNode)getTreeModel().getRoot();
            }
           
            private TreeSelectionModel getTreeSelectionModel(){
              return jtNetDevice.getSelectionModel();
            }
           

          3、根據(jù)node得到path:
            TreePath visiblePath = new TreePath(getTreeModel().getPathToRoot(node));

          4、根據(jù)Path展開到該節(jié)點
            jtNetDevice.makeVisible(visiblePath);

          5、根據(jù)path設定該節(jié)點選定
            jtNetDevice.setSelectionPath(visiblePath);

          6、選中節(jié)點的方法
            首先,根據(jù)節(jié)點得到樹路徑,其中chosen為需要選中的節(jié)點
            TreePath visiblePath = new TreePath( ( (DefaultTreeModel) jtNetDevice.getModel()).
                                                  getPathToRoot(chosen));
            然后根據(jù)Path選中該節(jié)點
            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
             // 創(chuàng)建樹
             JTree tree = new JTree();
            
             // 添加樹節(jié)點......
            
             // 遍歷所有節(jié)點
             visitAllNodes(tree);
            
             // 僅遍歷展開的節(jié)點
             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);
                     }
                 }
             }


          posted on 2006-04-04 17:24 SIMONE 閱讀(9202) 評論(1)  編輯  收藏 所屬分類: JAVA

          posted on 2011-03-23 08:32 Jamie 閱讀(1736) 評論(0)  編輯  收藏 所屬分類: swing


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 普兰县| 郸城县| 洪洞县| 冷水江市| 贺州市| 客服| 宣城市| 舒兰市| 甘洛县| 辽源市| 仙桃市| 兴山县| 南充市| 二连浩特市| 桐乡市| 南汇区| 南和县| 巨鹿县| 历史| 萨嘎县| 自贡市| 信阳市| 徐州市| 沙河市| 固原市| 犍为县| 阜阳市| 祥云县| 乌拉特前旗| 尖扎县| 华容县| 疏勒县| 商城县| 昂仁县| 宁海县| 松滋市| 万盛区| 北辰区| 海门市| 丽水市| 建阳市|