GUI图形编程(11)---树形菜单

GUI图形编程(11)---树形菜单_第1张图片

 GUI图形编程(11)---树形菜单_第2张图片

package org.lanqiao.menu.demo;

import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class JTreeTest {
	 public JTreeTest() {
	  JFrame f = new JFrame("TreeDemo");
	  f.setSize(300,500);
	  f.setLocationRelativeTo(null);
	  Container contentPane = f.getContentPane();//获取frame默认面板
	  //创建根节点
	  DefaultMutableTreeNode root = new DefaultMutableTreeNode("太原师范学院");
	  //创建一级节点
	  DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("计算机系");
	  DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("数学系");
	  DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("艺术系");
	  DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("中文系");
	  DefaultTreeModel model = new DefaultTreeModel(root);//指定树的模型
	  //创建二级节点
	  DefaultMutableTreeNode leafNode1 = new DefaultMutableTreeNode("计算机");
	  DefaultMutableTreeNode leafNode2 = new DefaultMutableTreeNode("师范");
	  DefaultMutableTreeNode leafNode3 = new DefaultMutableTreeNode("非师");
	  DefaultMutableTreeNode leafNode4 = new DefaultMutableTreeNode("物联");
	  //将节点添加到树模型中
	  model.insertNodeInto(node1, root,root.getChildCount());
	  model.insertNodeInto(node2,root, root.getChildCount());
	  model.insertNodeInto(node3,root, root.getChildCount());
	  model.insertNodeInto(node4,root, root.getChildCount());
	  model.insertNodeInto(leafNode1, node1, node1.getChildCount());
	  model.insertNodeInto(leafNode2, node1, node1.getChildCount());
	  model.insertNodeInto(leafNode3, node1, node1.getChildCount());
	  model.insertNodeInto(leafNode4, node1, node1.getChildCount());
	  JTree tree = new JTree(model);
	  //获取树的默认渲染器
	  DefaultTreeCellRenderer decr = (DefaultTreeCellRenderer) tree.getCellRenderer();
	  //设置节点图标
	  decr.setLeafIcon(new ImageIcon("image/1.png"));
	  decr.setOpenIcon(new ImageIcon("image/2.png"));
	  decr.setClosedIcon(new ImageIcon("image/3.gif"));
	  tree.setRowHeight(20);//设置行高
	  //使用滚动面板
	  JScrollPane scrollPane = new JScrollPane();
	  scrollPane.setViewportView(tree);
	 
	  contentPane.add(scrollPane);
	  f.pack();
	  f.setVisible(true);
	  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 /* f.addWindowListener(new WindowAdapter() {
	   public void windowClosing(WindowEvent e) {
	    System.exit(0);
	   }
	  });*/
	  //给树添加事件
	  tree.addTreeSelectionListener(new TreeSelectionListener() {
	   
	   @Override
	   public void valueChanged(TreeSelectionEvent e) {
	    DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree
	       .getLastSelectedPathComponent();
	    if(node.getLevel() ==2 ) {
	     System.out.println(node.getUserObject());//获取节点文本内容
	     //创建消息对话框   显示鼠标点击的节点的文本内容
	     if(node.getUserObject().equals("计算机")) {
	    	 JOptionPane.showMessageDialog(contentPane,node.getUserObject());
	     }
	     
	     	
	     
	    }
	    
	    
	   }
	  });
	 }
	 
	 public static void main(String args[]) {
	 
	  new JTreeTest();
	 }
	}

 

 

 

 

 

 

 

 

你可能感兴趣的:(GUI图形编程)