java 實現樹形結構

package tree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
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;

public class treenode extends JFrame implements TreeSelectionListener{

    JLabel jl;
    public treenode()
     {
        super("樹形結構");
        
        //建立根節點和子節點
        DefaultMutableTreeNode root=new DefaultMutableTreeNode("文本編輯器");
        
        
        DefaultMutableTreeNode node1=new DefaultMutableTreeNode("文件");
        DefaultMutableTreeNode node2=new DefaultMutableTreeNode("編輯");
        
        //利用根節點建立treemodel
        DefaultTreeModel treemodel=new DefaultTreeModel(root);
        //插入子節點
        treemodel.insertNodeInto(node1, root, root.getChildCount());
        treemodel.insertNodeInto(node2, root, root.getChildCount());
        
        //建立node1的子節點並插入
        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("關閉");
        treemodel.insertNodeInto(leafnode, node1, node1.getChildCount());
        
        //建立node2的子節點並插入
        leafnode=new DefaultMutableTreeNode("剪切");
        treemodel.insertNodeInto(leafnode, node2, node2.getChildCount());
        
        leafnode=new DefaultMutableTreeNode("複製");
        treemodel.insertNodeInto(leafnode, node2, node2.getChildCount());
        
        leafnode=new DefaultMutableTreeNode("粘貼");
        treemodel.insertNodeInto(leafnode, node2, node2.getChildCount());
        
        //建立樹對象
        JTree tree =new JTree(treemodel);
        
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.addTreeSelectionListener(this);
        tree.setRowHeight(20);
        
        //建立節點繪製對象
        DefaultTreeCellRenderer cell=(DefaultTreeCellRenderer)tree.getCellRenderer();
        
        //設置字體
        cell.setFont(new Font("Serif",Font.PLAIN,14));
        cell.setBackgroundNonSelectionColor(Color.white);
        cell.setBackgroundSelectionColor(Color.LIGHT_GRAY);
        cell.setForeground(Color.red);
        
        cell.setIcon(new ImageIcon("/image/folder.jpg"));
        
        
        //設置選中和不選中時文字的變化顏色
        cell.setTextNonSelectionColor(Color.black);
        cell.setTextNonSelectionColor(Color.black);
        
        
        this.add(new JScrollPane(tree));
        
        
        
         jl=new JLabel("你當前選擇的節點爲:",JLabel.CENTER);
        jl.setFont(new Font("Serif",Font.PLAIN,14));
        this.add(jl,BorderLayout.SOUTH);
        this.setSize(400, 400);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
     }
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        treenode t=new treenode();
    }


    @Override
    public void valueChanged(TreeSelectionEvent e) {
        // TODO Auto-generated method stub
        
        JTree tree=(JTree)e.getSource();
        
        //獲取當前選中的節點
        DefaultMutableTreeNode selectnode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
        String nodename=selectnode.toString();
        
        jl.setText("您當前選擇的節點爲:"+nodename);
        
        
        
        
    }

}
相關文章
相關標籤/搜索