[Swing]樹形結構的實現

風痕影默發表於2014-06-16

一般步驟:

1、建立根節點

private DefaultMutableTreeNode root = new DefaultMutableTreeNode("根節點");

2、建立樹形模型

private DefaultTreeModel treeModel = new DefaultTreeModel(root);

3、建立樹

private JTree tree = new JTree(treeModel);

4、向樹內新增節點

DefaultMutableTreeNode node = new DefaultMutableTreeNode("一級節點");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("二級節點");

/*
方法:insertNodeInto(DefaultMutableTreeNode child, DefaultMutableTreeNode parent, int index)
形參:child  所要插入的節點
形參:parent 插入節點的上級節點
形參: index  插入節點在父節點下的位置
*/
treeModel.insertNodeInto(node, root, root.getChildCount());
treeModel.insertNodeInto(node2, node, node.getChildCount());

5、新增節點監聽器

tree.addTreeSelectionListener(new TreeSelectionListener(){
       public void valueChanged(TreeSelectionEvent e){
           //監聽器操作程式碼
       }
});

6、新增顯示

JFrame jf = new JFrame("樹形結構");
jf.add(tree);
jf.setVisiable(true);

7、對樹屬性的一些描述方法舉例

//獲取樹節點渲染器
DefaultTreeCellRenderer cellRenderer = tree.getCellRenderer();
//設定未選擇時為黑色
cellRenderer.setTextNonSelectionColor(Color.black);
//設定選擇時為藍色
cellRenderer.setTextSelectionColor(Color.blue);
//設定選擇模式只能選擇一個
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

程式碼舉例:

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;


@SuppressWarnings("serial")
public class Test extends JFrame implements TreeSelectionListener{
    private DefaultMutableTreeNode root, node_1, node_2;
    private DefaultTreeModel treeModel;
    private JTree tree;
    public Test(){
        this.setSize(200, 150);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screenSize.width - this.getSize().width)/2;
        int y = (screenSize.height - this.getSize().height)/2;
        this.setLocation(x, y);
        
        root = new DefaultMutableTreeNode("根節點");
        node_1 = new DefaultMutableTreeNode("一級");
        node_2 = new DefaultMutableTreeNode("二級");
        
        treeModel = new DefaultTreeModel(root);
        treeModel.insertNodeInto(node_1, root, root.getChildCount());
        treeModel.insertNodeInto(node_2, node_1, node_1.getChildCount());
        
        tree = new JTree(treeModel);
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        
        DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
        cellRenderer.setTextNonSelectionColor(Color.black);
        cellRenderer.setTextSelectionColor(Color.blue);
        
        this.add(tree);
        this.setVisible(true);
    }

    public void valueChanged(TreeSelectionEvent e) {
        
    }
    public static void main(String[] args){
        @SuppressWarnings("unused")
        Test test = new Test();
    }
    
}

執行如下圖:

 

相關文章