一、JTree位於javax.swing.JTree這個包裡
使用構造方法JTree()方法創建出的樹是這樣的:
系統默認的根結點是JTree,在JTree下有3個子結點,這3個子結點屬於並列等級的:依次為colors、sports、food
以colors結點為根節點,colors結點下有4個子結點:依次是blue、violet、red、yellow
使用JTree()產生系統默認樹的具體例子:
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.JScrollPane;
public class InitialTree{
public InitialTree(){
JFrame frame = new JFrame("TreeDemo");
Container contentPane = frame.getContentPane();
JTree tree = new JTree();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
contentPane.add(scrollPane);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args){
new InitialTree();
}
}
這樣的話,系統默認的樹對於用戶具體的需求其實是沒有什麽用的。
所有可以使用JTree(Hashtable)構造一棵樹,需要注意的是使用JTree(Hashtable)構造的樹是不顯示根的
下面是使用JTree(Hashtable)構造一棵樹的例子
package test;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Hashtable;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.JTree;
public class TreeDemo01{
public TreeDemo01(){
JFrame frame = new JFrame("TreeDemo");
Container contentPane = frame.getContentPane();
String[] s1 = {"公司文件","個人信件","私人文件"};
String[] s2 = {"本地磁盤(C:)","本地磁盤(D:)","本地磁盤(E:)"};
String[] s3 = {"奇摩站","職棒消息","網絡書店"};
Hashtable hashtable1 = new Hashtable();//收藏夾和我的公文包是並列關係
Hashtable hashtable2 = new Hashtable();//hashtable2是hashtable1的子結點
hashtable1.put("我的公文包",s1);//hashtable1上有兩個子結點一個是收藏夾,另一個是我的公文包
hashtable2.put("我的電腦",s2);//hashtable2上有兩個子結點,一個是我的電腦,令一個是網站列表
hashtable1.put("收藏夾",hashtable2);
hashtable2.put("網站列表",s3);
JTree tree = new JTree(hashtable1);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
contentPane.add(scrollPane);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
public static void main(String[] args){
new TreeDemo01();
}
}
如果想做出一個顯示根節點的tree,可以使用JTree以下的構造方法:
JTree(TreeModel)
JTree(TreeNode)
JTree(TreeNode,Boolean)
二、JTree上的每一個結點就代表一個TreeNode、TreeNode本身是一個Interface ,TreeNode裏面定義了7中有關結點的方法,
Java提供了DefaultMutableTreeNode類,此類實現了MutableTreeNode接口,MultableTreeNode接口繼承了TreeNode接口。
DefaultMutableTreeNode的三個構造方法
DefaultMutableTreeNode():建立空的DefaultMutableTreeNode對象
DefaultMutableTreeNode(Object userObject):建立DefaultMutableTreeNode對象,結點為userObject
DefaultMutableTreeNode(Object userObject,Boolean allowsChildren):建立DefaultMutableTreeNode對象,結點為userObject,并決定是否允許此結點具有子結點
使用DefaultMutableTreeNode創建帶指定根結點的樹範例:
package test;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.JTree;
import javax.swing.JScrollPane;
public class TreeDemo02{
public TreeDemo02(){
JFrame frame = new JFrame("TreeDemo02");
Container contentPane = frame.getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("資源管理器");
DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("我的公文包");
DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("我的電腦");
DefaultMutableTreeNode node3 = new DefaultMutableTreeNode("收藏夾");
DefaultMutableTreeNode node4 = new DefaultMutableTreeNode("Readme");
root.add(node1);
root.add(node2);
root.add(node3);
root.add(node4);
DefaultMutableTreeNode leafnode = new DefaultMutableTreeNode("公司文件");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("個人信件");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("私人信件");
node1.add(leafnode);
leafnode = new DefaultMutableTreeNode("本地磁盤(C:)");
node2.add(leafnode);
leafnode = new DefaultMutableTreeNode("本地磁盤(D:)");
node2.add(leafnode);
leafnode = new DefaultMutableTreeNode("本地磁盤(E:)");
node2.add(leafnode);
DefaultMutableTreeNode node31 = new DefaultMutableTreeNode("網站列表");
node3.add(node31);
leafnode = new DefaultMutableTreeNode("奇摩站");
node31.add(leafnode);
leafnode = new DefaultMutableTreeNode("職棒消息");
node31.add(leafnode);
leafnode = new DefaultMutableTreeNode("網絡書店");
node31.add(leafnode);
JTree tree = new JTree(root);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
contentPane.add(scrollPane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new TreeDemo02();
}
}