Java SwingÖеÄJTreeÄ£ÐÍ
Java SwingÖеÄJTreeÄ£ÐÍ
2009-01-04 10:40
±¾ÎĽéÉÜJava SwingÖеÄJTreeÄ£ÐÍ£¬½éÉÜÈçºÎ´´½¨¡¢Ð޸ġ¢±éÀú£¬ÉèÖÃÊ÷¡£²Î¿¼Core Java2Ïà¹ØÕ½ڼ°ÆäÔ´´úÂë¡£
SwingÊ÷ʹÓÃÈËÃÇËùÊìϤµÄÎļþ¼ÐºÍÊ÷ҶͼÀ´ÏÔʾ·Ö²ãµÄÊý¾Ý¡£Ê÷Óɽڵã×é³É£¬½Úµã¿ÉÒÔÊÇÎļþ¼Ð£¬Ò²¿ÉÒÔÊÇÊ÷Ò¶¡£Îļþ¼Ð¿ÉÒÔÓÐ×ӽڵ㣬³ý¸ù½ÚµãÖ®ÍâµÄËùÓнڵ㶼ֻÓÐÒ»¸ö¸¸½Úµã¡£¿ÕµÄÎļþ¼ÐÓëÊ÷Ò¶µÄ²»Í¬Ö®´¦¾ÍÔÚÓÚËüÔÊÐíÓÐ×ӽڵ㡣
³ý¸¸½ÚµãºÍ×Ó½ÚµãÍ⣬Ê÷µÄ½Úµã»¹ÓÐÒ»¸öÓû§¶ÔÏó(µ±Ê¹ÓÃDefaultTreeModelʱ¾Í»á³ÊÏÖÒ»¸öÓû§¶ÔÏó)¡£Óû§¶ÔÏóÊÇObjectÀàÐÍ£¬Òò´ËËüÌṩÁËÒ»¸ö½«ÈÎÒâ¶ÔÏóÓë½ÚµãÏà¹ØÁªµÄ°ì·¨¡£
Ê÷ÓÐÒ»¸ö¼òµ¥µÄÄ£ÐÍ£¬Ã¿Ò»¸öJTreeʵÀý¶¼ÒªÎ¬»¤¶Ô»æÖÆÆ÷ºÍ±à¼Æ÷µÄÒýÓã¬Õâ¸ö»æÖÆÆ÷ºÍ±à¼Æ÷±»Ê÷ÖÐËùÓеĽڵãËùʹÓᣱí1ÖÐÁгöÁËswing.tree°üÖеÄÖ÷ÒªÀà¡£
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡±í1 Swing.tree°üÖеÄÖ÷ÒªÀà
¡¡Ãû¡¡³Æ ʵ¡¡ÏÖ
DefaultMutableTreeNode Ò»¸ö¾ßÓÐÒ»¸ö¸¸½Úµã¡¢£¨¿ÉÄÜ£©Ðí¶à×Ó½ÚµãºÍ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Ò»¸öÓû§¶ÔÏóµÄ¿É±ä½Úµã£¬ÎªÏà¹ØÁªµÄ½ÚµãÌṩÁË·ÃÎÊ·½·¨¡£Èç¹ûûÓÐÈκÎ×ӽڵ㣬Õâ¸ö½Úµã¾ÍÊÇÊ÷Ò¶¡£
DefaultTreeModel Ò»¸ö¼¤·¢TreeModel EventsʼþµÄ¼òµ¥¿É±äµÄÄ£ÐÍ¡£Ìṩ¶Ô×Ó½ÚµãµÄ·ÃÎÊ·½·¨£¬µ«²»ÊÇÌṩ¶Ô¸¸½ÚµãµÄ·ÃÎÊ·½·¨¡£
DefaultTreeCellEditor »æÖÆÆ÷ºÍ±à¼Æ÷µÄ°ü×°Æ÷£¬Ëü°ÑÒ»¸ö“ÕæÕý”µÄ±à¼Æ÷×é¼þ·ÅÔÚ½Úµãͼ±êµÄÅԱߡ£
DefaultTreeCellRenderer ¾ßÓÐ×ÖÌå¡¢ÑÕÉ«ºÍͼ±ê·ÃÎÊ·½·¨µÄJLabelÀ©¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Õ¹£¬ËüÌṩͼ±êµÄȱʡֵ¡£
TreePath ÓÉÒ»¸ö½Úµãµ½ÁíÒ»¸ö½ÚµãµÄ·¾¶¡£Â·¾¶ÖеĽڵã´æ´¢ÔÚÒ»¸öÊý×éÖС£Â·¾¶ÓÃÓÚÔÚÑ¡È¡ÄÚÈÝÖ®¼ä½øÐÐͨÐÅ¡£
1£®¹¹½¨Ê÷Ä£ÐÍ
Ïêϸ´úÂë¼ûSimpleTree.java
DefaultMutableTreeNode root = new DefaultMutableTreeNode("World");
DefaultMutableTreeNode country = new DefaultMutableTreeNode("USA");
root.add(country);
¡£¡£¡£
JTree tree = new JTree(root);
Container contentPane = getContentPane();
contentPane.add(new JScrollPane(tree));
ÕâЩ´úÂëÀ´×ÔÒ»¸öJFrameµÄ¹¹Ôì×ÓÖС£ÕâÀïͨ¹ýJTreeÀ´´´½¨Ò»¸öTreeModelµÄʵÀý¡£
2£®Ê÷½áµã
Ê÷½ÚµãÓÉTreeNode½Ó¿Ú¶¨Ò壬TreeNode½Ó¿Ú±»MutableTreeNode½Ó¿ÚÀ©Õ¹£¬¶øMutableTreeNode½Ó¿ÚÓÖÓÉ DefaultMutableTreeNodeÀàÀ´ÊµÏÖ¡£
Ê÷½á¹¹ÏêϸÀàͼ
2.1 TreeNode½Ó¿Ú
TreeNode½Ó¿Ú¶¨ÒåÁË£¨¹Ì¶¨£©Ê÷½ÚµãµÄʵÖÊ£¬±í2×ܽáÁËTreeNodeÏà¹Ø·
Ïà¹ØÎĵµ£º
JAVA×¢ÊÍ·½·¨¼°¸ñʽ
1¡¢µ¥ÐÐ(single-line)--¶Ì×¢ÊÍ£º//……
µ¥¶ÀÐÐ×¢ÊÍ£ºÔÚ´úÂëÖе¥ÆðÒ»ÐÐ×¢ÊÍ£¬ ×¢ÊÍÇ°×îºÃÓÐÒ»ÐпÕÐУ¬²¢ÓëÆäºóµÄ´úÂë¾ßÓÐÒ»ÑùµÄËõ½ø²ã¼¶¡£Èç¹ûµ¥ÐÐÎÞ·¨Íê³É£¬ÔòÓ¦²ÉÓÿé×¢ÊÍ¡£
×¢Ê͸ñʽ£º/* ×¢ÊÍÄÚÈÝ */
ÐÐÍ·×¢ÊÍ£ºÔÚ´úÂëÐеĿªÍ·½øÐÐ×¢ÊÍ¡£Ö÷ҪΪÁËʹ¸ÃÐд ......
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import j ......
ת×Ô£ºhttp://blog.163.com/gordonkkk/blog/static/63425684200962795039630/
´ÓCSDNÉÏ¿´µ½Ò»ÆªÅúÆÀJavaÓïÑÔÖî¶àÎÊÌâµÄ·ÒëÎÄÕ£¬ÔÎÄ×÷ÕßÊÇMario Fusco¡£ËûÖ¸³öÁËJavaÓïÑÔÂäºóÓÚʱ´ú£¬»ýÖØÄÑ·µµÄ10¸öÎÊÌâ¡£¿´¹ýÖ®ºóÒýÆðÁËÎÒµÄһЩÁªÏë¡£ÏÂÃæÁгöËûËù˵µÄ10´óÎÊÌâ¡£½Û»ÆÉ«×ÖÌåÊÇÎÒÒ»µãµã²»³ÉÊìµÄ˼¿¼¡£
1¡¢È±ÉÙ±Õ°ü£¨clos ......
µÚÒ»£¬Ì¸Ì¸final, finally, finalizeµÄÇø±ð¡£
¡¡final ÓÃÓÚÉùÃ÷ÊôÐÔ£¬·½·¨ºÍÀ࣬·Ö±ð±íʾÊôÐÔ²»¿É±ä£¬·½·¨²»¿É¸²¸Ç£¬À಻¿É¼Ì³Ð¡£finallyÊÇÒì³£´¦ÀíÓï¾ä½á¹¹µÄÒ»²¿·Ö£¬±íʾ×ÜÊÇÖ´ÐС£finalizeÊÇ ObjectÀàµÄÒ»¸ö·½·¨£¬ÔÚÀ¬»øÊÕ¼¯Æ÷Ö´ÐеÄʱºò»áµ÷Óñ»»ØÊÕ¶ÔÏóµÄ´Ë·½·¨£¬¿ÉÒÔ¸²¸Ç´Ë·½·¨ÌṩÀ¬»øÊÕ¼¯Ê± ......