JTree 与Swing 的综合应用

1. Swing 中JTree 的使用

 

    结果为

 

    结果

 

 

package org.kjcx.liuqing.swing.test;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * 
 * @author LiuQing
 * @version 1.0
 * @see 
 * @date 2010-8-10上午09:28:20
 */
public class UserManagerUI extends JFrame implements TreeSelectionListener{
	
	private static final long serialVersionUID = 4228671843583729084L;

	private JTextArea evnContext;
	
	private JScrollPane evnPane;
	
	private JTextField enterInfo;
	
	private JButton sendButton;
	
	private JPanel southPanel;
	
	private JTree tree;
	
	private JScrollPane treePane;
	
	private JSplitPane sp;
	
	private JToolBar toolBar;
	
	private JButton add;
	
	private JMenuBar menuBar;
	
	private JMenu fileMenu;
	
	private JMenuItem openItem,runningItem,closeItem;
	
	private JSeparator fileSp;
	
	private JFileChooser chooser;
	
	
	public UserManagerUI() {
		
		
		menuBar = new JMenuBar();
		fileMenu = new JMenu("File");
		openItem = new JMenuItem("open");
		runningItem = new JMenuItem("running");
		closeItem = new JMenuItem("close");
		fileSp = new JSeparator();
		openItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				chooser.showOpenDialog(openItem);
			}
			
		});
		
		fileMenu.add(openItem);
		fileMenu.add(runningItem);
		fileMenu.add(fileSp);
		fileMenu.add(closeItem);
		//文件选择的使用
		this.chooser = new JFileChooser();
		chooser.addChoosableFileFilter(new FileFilter(){
			
			@Override
			public boolean accept(File f) {
				if (f.isDirectory()) {
					return true;
				}
				return f.getName().endsWith(".class") 
					|| f.getName().endsWith(".jpg") 
					|| f.getName().endsWith(".gif");
			}

			@Override
			public String getDescription() {
				return "*.class *.gif *.jpg";
			}
			
		});
		
		menuBar.add(fileMenu);
		this.setJMenuBar(menuBar);
		closeItem.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				System.exit(-1);
			}
			
		});
		
		toolBar = new JToolBar();
		add = new JButton("addition");
		toolBar.add(add);
		
		this.evnContext = new JTextArea();
		this.evnPane = new JScrollPane(evnContext);
		this.enterInfo = new JTextField(10);
		this.sendButton = new JButton("send Message");
		this.southPanel = new JPanel();
		NodeTree node = new NodeTree("1","用户管理");
		NodeTree node2 = new NodeTree("2","用户类型");
		NodeTree node3 = new NodeTree("3","产品类型");
		NodeTree node4 = new NodeTree("4","订单信息");
		DefaultMutableTreeNode a_node = new DefaultMutableTreeNode(node);
		DefaultMutableTreeNode a_node2 = new DefaultMutableTreeNode(node2);
		DefaultMutableTreeNode a_node3 = new DefaultMutableTreeNode(node3);
		DefaultMutableTreeNode a_node4 = new DefaultMutableTreeNode(node4);
		
		a_node.add(a_node2);
		a_node.add(a_node3);
		a_node3.add(a_node4);
		tree = new JTree(a_node);
		tree.addTreeSelectionListener(this);
		treePane = new JScrollPane(tree);
		this.sp =  new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,treePane,evnPane);
		this.sp.setOneTouchExpandable(true);
		southPanel.add(enterInfo);
		southPanel.add(sendButton);
		
		this.add(sp);
		this.add(toolBar,BorderLayout.NORTH);
		this.add(southPanel,BorderLayout.SOUTH);
		
		this.setVisible(true);
		this.setSize(400,400);
		this.setTitle("QQ");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
	}
	
	public static void main(String args[]) throws Exception {
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		new UserManagerUI();
	}
	
	/**
	 * 
	 * @author LiuQing
	 * @version 1.0
	 * @see 
	 * @date 2010-8-15上午09:34:25
	 */
	class NodeTree {
		
		private String id;
		
		private String name;
		
		private NodeTree parentNode;

		public NodeTree() {
			super();
		}

		public NodeTree(String id, String name) {
			super();
			this.id = id;
			this.name = name;
		}
		
		public String getId() {
			return id;
		}

		public void setId(String id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public NodeTree getParentNode() {
			return parentNode;
		}

		public void setParentNode(NodeTree parentNode) {
			this.parentNode = parentNode;
		}

		@Override
		public String toString() {
			return name;
		}
		
	}

	/**
	 * 
	 */
	public void valueChanged(TreeSelectionEvent e) {
		//获得树的结点路径
	    DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)
	    				e.getNewLeadSelectionPath().getLastPathComponent();
	    //获得用户对应的结点对象
	    NodeTree nodeTree = (NodeTree)treeNode.getUserObject();
	    this.evnContext.setText(nodeTree.getName());
	}

}

你可能感兴趣的:(swing)