[Swing]树形结构的实现

时间:2014-06-18 14:13:59   收藏:0   阅读:273

一般步骤:

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

运行如下图:

 

bubuko.com,布布扣

[Swing]树形结构的实现,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!