_chapter 6.htm
来自「Core Java 2(中文名称:JAVA 2 核心技术 卷二:高级特性)这是英」· HTM 代码 · 共 1,429 行 · 第 1/5 页
HTM
1,429 行
8. */
9. public class SimpleTree
10. {
11. public static void main(String[] args)
12. {
13. JFrame frame = new SimpleTreeFrame();
14. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
15. frame.show();
16. }
17. }
18.
19. /**
20. This frame contains a simple tree that displays a
21. manually constructed tree model.
22. */
23. class SimpleTreeFrame extends JFrame
24. {
25. public SimpleTreeFrame()
26. {
27. setTitle("SimpleTree");
28. setSize(WIDTH, HEIGHT);
29.
30. // set up tree model data
31.
32. DefaultMutableTreeNode root
33. = new DefaultMutableTreeNode("World");
34. DefaultMutableTreeNode country
35. = new DefaultMutableTreeNode("USA");
36. root.add(country);
37. DefaultMutableTreeNode state
38. = new DefaultMutableTreeNode("California");
39. country.add(state);
40. DefaultMutableTreeNode city
41. = new DefaultMutableTreeNode("San Jose");
42. state.add(city);
43. city = new DefaultMutableTreeNode("Cupertino");
44. state.add(city);
45. state = new DefaultMutableTreeNode("Michigan");
46. country.add(state);
47. city = new DefaultMutableTreeNode("Ann Arbor");
48. state.add(city);
49. country = new DefaultMutableTreeNode("Germany");
50. root.add(country);
51. state = new DefaultMutableTreeNode("Schleswig-Holstein");
52. country.add(state);
53. city = new DefaultMutableTreeNode("Kiel");
54. state.add(city);
55.
56. // construct tree and put it in a scroll pane
57.
58. JTree tree = new JTree(root);
59. Container contentPane = getContentPane();
60. contentPane.add(new JScrollPane(tree));
61. }
62.
63. private static final int WIDTH = 300;
64. private static final int HEIGHT = 200;
65. }
</pre>
<p class="docText">When you run the program, the tree first looks as in
<a class="docLink" href="#ch06fig09">Figure 6-9</a>. Only the root node and its
children are visible. Click on the circle icons (the <span class="docEmphasis">
handles</span>) to open up the subtrees. The line sticking out from the handle
icon points to the right when the subtree is collapsed, and it points down when
the subtree is expanded (see <a class="docLink" href="#ch06fig10">Figure 6-10</a>).
We don't know what the designers of the Metal look and feel had in mind, but we
think of the icon as a door handle. You push down on the handle to open the
subtree.</p>
<center>
<h5 id="ch06fig09" class="docFigureTitle">Figure 6-9. The initial tree display</h5>
<p>
<img alt="graphics/06fig09.gif" src="06fig09.gif" border="0" width="301" height="198"></p>
</center><center>
<h5 id="ch06fig10" class="docFigureTitle">Figure 6-10. Collapsed and expanded subtrees</h5>
<p>
<img alt="graphics/06fig10.gif" src="06fig10.gif" border="0" width="500" height="230"></p>
</center>
<div class="docNote">
<p class="docNoteTitle">NOTE</p>
<table cellSpacing="0" cellPadding="1" width="90%" border="0">
<tr>
<td vAlign="top" width="60">
<img alt="graphics/note.gif" src="note.gif" align="left" border="0" width="54" height="53"><br>
</td>
<td vAlign="top">
<p class="docText">Of course, the display of the tree depends on the
selected look and feel. We just described the Java look and feel (a.k.a.
"Metal"). In the Windows and Motif look and feel, the handles have the
more familiar look梐 "+" or "-" in a box (see
<a class="docLink" href="#ch06fig11">Figure 6-11</a>).</p>
<center>
<h5 id="ch06fig11" class="docFigureTitle">Figure 6-11. A tree with the Windows look and
feel</h5>
<p>
<img alt="graphics/06fig11.gif" src="06fig11.gif" border="0" width="413" height="196"><br>
</p>
</center></td>
</tr>
</table>
</div>
<p class="docText">Up to SDK 1.3, the Java look and feel does not display the
tree outline by default (see <a class="docLink" href="#ch06fig12">Figure 6-12</a>).
As of SDK 1.4, the default line style is "angled."</p>
<center>
<h5 id="ch06fig12" class="docFigureTitle">Figure 6-12. A tree with no connecting lines</h5>
<p>
<img alt="graphics/06fig12.gif" src="06fig12.gif" border="0" width="301" height="198"></p>
</center>
<p class="docText">In SDK 1.4, use the following magic incantation to turn off
the lines joining parents and children:</p>
<pre>tree.putClientProperty("JTree.lineStyle", "None");
</pre>
<p class="docText">Conversely, to make sure that the angled lines are shown, use</p>
<pre>tree.putClientProperty("JTree.lineStyle", "Angled");
</pre>
<p class="docText">There is also another line style, <tt>"Horizontal</tt>", that
is shown in <a class="docLink" href="#ch06fig13">Figure 6-13</a>. The tree is
displayed with horizontal lines separating only the children of the root. We
aren't quite sure what it is good for.</p>
<center>
<h5 id="ch06fig13" class="docFigureTitle">Figure 6-13. A tree with the horizontal line style</h5>
<p>
<img alt="graphics/06fig13.gif" src="06fig13.gif" border="0" width="301" height="198"></p>
</center>
<p class="docText">By default, there is no handle for collapsing the root of the
tree. If you like, you can add one with the call</p>
<pre>tree.setShowsRootHandles(true);
</pre>
<p class="docText"><a class="docLink" href="#ch06fig14">Figure 6-14</a> shows
the result. Now you can collapse the entire tree into the root node.</p>
<center>
<h5 id="ch06fig14" class="docFigureTitle">Figure 6-14. A tree with a root handle</h5>
<p>
<img alt="graphics/06fig14.gif" src="06fig14.gif" border="0" width="476" height="227"></p>
</center>
<p class="docText">Conversely, you can hide the root altogether. You do that to
display a <span class="docEmphasis">forest,</span> a set of trees, each of which
has its own root. You still must join all trees in the forest to a common root.
Then, you hide the root with the instruction</p>
<pre>tree.setRootVisible(false);
</pre>
<p class="docText">Look at <a class="docLink" href="#ch06fig15">Figure 6-15</a>.
There appear to be two roots, labeled "USA" and "Germany." The actual root that
joins the two is made invisible.</p>
<center>
<h5 id="ch06fig15" class="docFigureTitle">Figure 6-15. A forest</h5>
<p>
<img alt="graphics/06fig15.gif" src="06fig15.gif" border="0" width="301" height="198"></p>
</center>
<p class="docText">Let's turn from the root to the leaves of the tree. Note that
the leaves have a different icon than the other nodes (see
<a class="docLink" href="#ch06fig16">Figure 6-16</a>).</p>
<center>
<h5 id="ch06fig16" class="docFigureTitle">Figure 6-16. Leaf icons</h5>
<p>
<img alt="graphics/06fig16.gif" src="06fig16.gif" border="0" width="350" height="177"></p>
</center>
<p class="docText">When displaying the tree, each node is drawn with an icon.
There are actually three kinds of icons: a leaf icon, an opened non-leaf icon,
and a closed non-leaf icon. For simplicity, we'll refer to the last two as
folder icons.</p>
<p class="docText">The node renderer needs to know which icon to use for each
node. By default, the decision process works like this: If the <tt>isLeaf</tt>
method of a node returns <tt>true</tt>, then the leaf icon is used. Otherwise, a
folder icon is used.</p>
<p class="docText">The <tt>isLeaf</tt> method of the <tt>DefaultMutableTreeNode</tt>
class returns <tt>true</tt> if the node has no children. Thus, nodes with
children get folder icons, and nodes without children get leaf icons.</p>
<p class="docText">Sometimes, that behavior is not appropriate. Suppose we added
a node "Montana" to our sample tree, but we're at a loss as to what cities to
add. We would not want the state node to get a leaf icon since conceptually only
the cities are leaves.</p>
<p class="docText">The <tt>JTree</tt> class has no idea which nodes should be
leaves. It asks the tree model. If a childless node isn't automatically a
conceptual leaf, you can ask the tree model to use a different criterion for
leafiness, namely to query the "allows children" node property.</p>
<p class="docText">For those nodes that should not have children, call</p>
<pre>node.setAllowsChildren(false);
</pre>
<p class="docText">Then, tell the tree model to ask the value of the "allows
children" property to determine whether a node should be displayed with a leaf
icon. You use the <tt>setAsksAllowsChildren</tt> method of the <tt>
DefaultTreeModel</tt> class to set this behavior:</p>
<pre>model.setAsksAllowsChildren(true);
</pre>
<p class="docText">With this decision criterion, nodes that allow children get
folder icons, and nodes that don't allow children get leaf icons.</p>
<p class="docText">Alternatively, if you construct the tree by supplying the
root node, supply the setting for the "asks allows children" property in the
constructor.</p>
<pre>JTree tree = new JTree(root, true);
// nodes that don't allow children get leaf icons
</pre>
<h5 class="docSection3Title" id="ch06lev3sec9"><tt>javax.swing.JTree</tt></h5>
<p><img alt="graphics/api.gif" src="api.gif" border="0" width="46" height="45"><br>
</p>
<ul>
<li>
<p class="docList"><tt>JTree(TreeModel model)</tt></p>
<p class="docList">constructs a tree from a tree model.</li>
<li>
<p class="docList"><tt>JTree(TreeNode root)</tt></li>
<li>
<p class="docList"><tt>JTree(TreeNode root, boolean asksAllowChildren)</tt></p>
<p class="docList">construct a tree with a default tree model that displays
the root and its children.</p>
<table cellSpacing="0" cellPadding="1" width="93%" border="1">
<colgroup span="3" align="left">
</colgroup>
<tr>
<td class="docTableCell" vAlign="top"><span class="docEmphasis">
Parameters:</span></td>
<td class="docTableCell" vAlign="top"><tt>root</tt></td>
<td class="docTableCell" vAlign="top">The root node</td>
</tr>
<tr>
<td class="docTableCell" vAlign="top"> </td>
<td class="docTableCell" vAlign="top"><tt>asksAllowsChildren</tt></td>
<td class="docTableCell" vAlign="top"><tt>true</tt> to use the "allows
children" node property for determining whether a node is a leaf</td>
</tr>
</table>
<p> </li>
<li>
<p class="docList"><tt>void setShowsRootHandles(boolean b)</tt></p>
<p class="docList">If <tt>b</tt> is <tt>true</tt>, then the root node has a
handle for collapsing or expanding its children.</li>
<li>
<p class="docList"><tt>void setRootVisible(boolean b)</tt></p>
<p class="docList">If <tt>b</tt> is true, then the root node is displayed.
Otherwise, it is hidden.</li>
</ul>
<h5 class="docSection3Title" id="ch06lev3sec10"><span class="docEmphasis"><tt>
javax.swing.tree.TreeNode</tt></span></h5>
<p><img alt="graphics/api.gif" src="api.gif" border="0" width="46" height="45"><br>
</p>
<ul>
<li>
<p class="docList"><tt>boolean isLeaf()</tt></p>
<p class="docList">returns <tt>true</tt> if this node is conceptually a leaf.</li>
<li>
<p class="docList"><tt>boolean getAllowsChildren()</tt></p>
<p class="docList">returns <tt>true</tt> if this node can have child nodes.</li>
</ul>
<h5 class="docSection3Title" id="ch06lev3sec11"><span class="docEmphasis"><tt>
javax.swing.tree.MutableTreeNode</tt></span></h5>
<p><img alt="graphics/api.gif" src="api.gif" border="0" width="46" height="45"><br>
</p>
<ul>
<li>
<p class="docList"><tt>void setUserObject(Object userObject)</tt></p>
<p class="docList">sets the "user object" that the tree node uses for
rendering.</li>
</ul>
<h5 class="docSection3Title" id="ch06lev3sec12"><span class="docEmphasis"><tt>
javax.swing.tree.TreeModel</tt></span></h5>
<p><img alt="graphics/api.gif" src="api.gif" border="0" width="46" height="45"><br>
</p>
<ul>
<li>
<p class="docList"><tt>boolean isLeaf(Object node)</tt></p>
<p class="docList">returns <tt>true</tt> if <tt>node</tt> should be displayed
as a leaf node.</li>
</ul>
<h5 class="docSection3Title" id="ch06lev3sec13"><tt>javax.swing.tree.DefaultTreeModel</tt></h5>
<p><img alt="graphics/api.gif" src="api.gif" border="0" width="46" height="45"><br>
</p>
<ul>
<li>
<p class="docList"><tt>void setAsksAllowsChildren(boolean b)</tt></p>
<p class="docList">If <tt>b</tt> is <tt>true</tt>, then nodes are displayed as
leaves when their <tt>getAllowsChildren</tt> method returns <tt>false</tt>.
Otherwise, they are displayed as leaves when their <tt>isLeaf</tt> method
returns <tt>true</tt>.</li>
</ul>
<h5 class="docSection3Title" id="ch06lev3sec14"><tt>javax.swing.tree.DefaultMutableTreeNode</tt></h5>
<p><img alt="graphics/api.gif" src="api.gif" border="0" width="46" height="45"><br>
</p>
<ul>
<li>
<p class="docList"><tt>DefaultMutableTreeNode(Object userObject)</tt></p>
<p class="docList">constructs a mutable tree node with the given user object.</li>
<li>
<p class="docList"><tt>v
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?