Swing Tree

Swing Tree
package  GUI;

import  java.awt.Dimension;

import  javax.swing.JFrame;
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.TreeSelectionModel;

public   class  TreeTest  extends  JFrame  {

    
public TreeTest() {
        DefaultMutableTreeNode Program 
= new DefaultMutableTreeNode("Program");
        DefaultMutableTreeNode Office 
= new DefaultMutableTreeNode("Office");
        DefaultMutableTreeNode Word 
= new DefaultMutableTreeNode("Word");
        DefaultMutableTreeNode PowerPoint 
= new DefaultMutableTreeNode("PowerPoint");
        DefaultMutableTreeNode Access 
= new DefaultMutableTreeNode("Access");
        DefaultMutableTreeNode Excel 
= new DefaultMutableTreeNode("Excel");
        DefaultMutableTreeNode Java 
= new DefaultMutableTreeNode("Java");
        DefaultMutableTreeNode j2se 
= new DefaultMutableTreeNode("j2se");
        DefaultMutableTreeNode j2ee 
= new DefaultMutableTreeNode("j2ee");
        DefaultMutableTreeNode j2me 
= new DefaultMutableTreeNode("j2me");
        DefaultMutableTreeNode Application 
= new DefaultMutableTreeNode("Application");
        DefaultMutableTreeNode Dreamweaver 
= new DefaultMutableTreeNode("Dreamweaver");
        DefaultMutableTreeNode PotoShop 
= new DefaultMutableTreeNode("PotoShop");
        DefaultMutableTreeNode VisualStudio 
= new DefaultMutableTreeNode("VisualStudio");
        
        Office.add(Word);
        Office.add(PowerPoint);
        Office.add(Access);
        Office.add(Excel);
        Java.add(j2se);
        Java.add(j2ee);
        Java.add(j2me);
        Application.add(Dreamweaver);
        Application.add(PotoShop);
        Application.add(VisualStudio);
        Program.add(Office);
        Program.add(Java);
        Program.add(Application);
        
        JTree jtree 
= new JTree(Program);
        jtree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
        jtree.setRowHeight(
20);

        jtree.addTreeSelectionListener(
new TreeSelectionListener() {
            
public void valueChanged(TreeSelectionEvent e) {
                JTree tree 
= (JTree)e.getSource();
                System.out.println(e.getPath());
            }

        }
);

        
        JScrollPane jscrollpane 
= new JScrollPane(jtree);
        
        
this.add(jscrollpane);
    }

    
    
public static void main(String[] args) {
        TreeTest tt 
= new TreeTest();
        tt.setBounds(
100200300400);
        tt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tt.setVisible(
true);
        
    }

    
}



你可能感兴趣的:(Swing Tree)