[Swing]樹形結構的實現

通常步驟:java

一、創建根節點node

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

二、創建樹形模型this

private DefaultTreeModel treeModel = new DefaultTreeModel(root);

三、創建樹spa

private JTree tree = new JTree(treeModel);

四、向樹內添加節點code

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());

五、添加節點監聽器blog

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

六、添加顯示get

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

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

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

代碼舉例:io

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();
    }
    
}

運行以下圖:event

 

相關文章
相關標籤/搜索