首页>代码>100个java swing开发的GUI程序>/100个GUI程序/exam/ch10/TreeDemo4.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TreeDemo4
{
    public TreeDemo4()
    {
        JFrame f = new JFrame("TreeDemo");
        Container contentPane = f.getContentPane();
        
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("资源管理器");
        DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("我的公文包");
        DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("我的电脑");
        DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("收藏夹");
        DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Readme");
        
        DefaultTreeModel treeModel = new DefaultTreeModel(root);
        treeModel.insertNodeInto(node1, root, root.getChildCount());
        treeModel.insertNodeInto(node2, root, root.getChildCount());
        treeModel.insertNodeInto(node3, root, root.getChildCount());
        treeModel.insertNodeInto(node4, root, root.getChildCount());
        
        DefaultMutableTreeNode leafnode = new 
                DefaultMutableTreeNode("公司文件");
        treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());
        leafnode = new DefaultMutableTreeNode("个人信件");
        treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());
        leafnode = new DefaultMutableTreeNode("私人文件");
        treeModel.insertNodeInto(leafnode, node1, node1.getChildCount());
        
        leafnode = new DefaultMutableTreeNode("本机磁盘(C:)");
        treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());
        leafnode = new DefaultMutableTreeNode("本机磁盘(D:)");
        treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());
        leafnode = new DefaultMutableTreeNode("本机磁盘(E:)");
        treeModel.insertNodeInto(leafnode, node2, node2.getChildCount());
        
        DefaultMutableTreeNode node31 = new DefaultMutableTreeNode("网站列表");
        treeModel.insertNodeInto(node31, node3, node3.getChildCount());
        leafnode = new DefaultMutableTreeNode("奇摩站");
        treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());
        leafnode = new DefaultMutableTreeNode("职棒消息");
        treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());
        leafnode = new DefaultMutableTreeNode("网络书店");
        treeModel.insertNodeInto(leafnode, node3, node3.getChildCount());
        
        JTree tree = new JTree(treeModel);
        tree.setRowHeight(20); 
        
        DefaultTreeCellRenderer cellRenderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
        
        cellRenderer.setLeafIcon(new ImageIcon(".\\icons\\leaf.gif"));
        cellRenderer.setOpenIcon(new ImageIcon(".\\icons\\open.gif"));
        cellRenderer.setClosedIcon(new ImageIcon(".\\icons\\close.gif"));

        cellRenderer.setFont(new Font("宋体",Font.PLAIN,12));
        cellRenderer.setBackgroundNonSelectionColor(Color.white);
        cellRenderer.setBackgroundSelectionColor(Color.yellow);
        cellRenderer.setBorderSelectionColor(Color.red);
        cellRenderer.setTextNonSelectionColor(Color.black);
        cellRenderer.setTextSelectionColor(Color.blue);
        
        tree.setEditable(true);
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportView(tree);
        
        contentPane.add(scrollPane);
        f.pack();
        f.setVisible(true);
        
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

    public static void main(String args[]) {
    
        new TreeDemo4();
    }
}

最近下载更多
fan2019  LV3 1月12日
yangxb2  LV10 2023年10月20日
微信网友_6639502556778496  LV1 2023年9月7日
微信网友_6292282163859456  LV4 2023年1月5日
你们的代码都是我的了  LV16 2022年12月14日
wadadd  LV7 2022年9月4日
annunx  LV1 2022年4月15日
微信网友_5852742079762432  LV6 2022年3月5日
sswert  LV2 2022年2月22日
双方各何必呢  LV13 2021年12月20日
最近浏览更多
20201202038 3月27日
暂无贡献等级
晨爽明宇  LV1 3月4日
Gin19960217  LV4 1月15日
fan2019  LV3 1月12日
1053001914  LV1 2023年12月28日
求学的熊猫  LV6 2023年12月25日
一眼丁真  LV1 2023年12月16日
shiaomon 2023年12月14日
暂无贡献等级
fesfefe  LV13 2023年11月25日
卜算子 2023年10月30日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友