📄 treesframe.html
字号:
<html><head><title>Display page for Trees at www.BruceEckel.com</title></head>
<FONT FACE="Verdana,Tahoma,Arial,Helvetica,Sans" SIZE="-1">
<FONT FACE="Verdana,Tahoma,Arial,Helvetica,Sans"><H2>Trees.java</H2><hr></FONT>
<!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<OBJECT
classid = "clsid:CAFEEFAC-0014-0001-0001-ABCDEFFEDCBA"
codebase = "http://java.sun.com/products/plugin/autodl/jinstall-1_4_1_01-windows-i586.cab#Version=1,4,1,1"
WIDTH = "250" HEIGHT = "250" ALIGN = "baseline" >
<PARAM NAME = CODE VALUE = "Trees.class" >
<PARAM NAME = "type" VALUE = "application/x-java-applet;jpi-version=1.4.1_01">
<PARAM NAME = "scriptable" VALUE = "false">
<COMMENT>
<EMBED
type = "application/x-java-applet;jpi-version=1.4.1_01"
CODE = "Trees.class"
WIDTH = "250"
HEIGHT = "250"
ALIGN = "baseline"
scriptable = false
pluginspage = "http://java.sun.com/products/plugin/index.html#download">
<NOEMBED>
</NOEMBED>
</EMBED>
</COMMENT>
</OBJECT>
<!--
<APPLET CODE = "Trees.class" WIDTH = "250" HEIGHT = "250" ALIGN = "baseline">
</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->
<FONT SIZE="+3"><PRE><B>
//: c14:Trees.java
// Simple Swing tree. Trees can be vastly more complex.
// <applet code=Trees width=250 height=250></applet>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.tree.*;
import com.bruceeckel.swing.*;
// Takes an array of Strings and makes the first
// element a node and the rest leaves:
class Branch {
private DefaultMutableTreeNode r;
public Branch(String[] data) {
r = new DefaultMutableTreeNode(data[0]);
for(int i = 1; i < data.length; i++)
r.add(new DefaultMutableTreeNode(data[i]));
}
public DefaultMutableTreeNode node() { return r; }
}
public class Trees extends JApplet {
private String[][] data = {
{ "Colors", "Red", "Blue", "Green" },
{ "Flavors", "Tart", "Sweet", "Bland" },
{ "Length", "Short", "Medium", "Long" },
{ "Volume", "High", "Medium", "Low" },
{ "Temperature", "High", "Medium", "Low" },
{ "Intensity", "High", "Medium", "Low" },
};
private static int i = 0;
private DefaultMutableTreeNode root, child, chosen;
private JTree tree;
private DefaultTreeModel model;
public void init() {
Container cp = getContentPane();
root = new DefaultMutableTreeNode("root");
tree = new JTree(root);
// Add it and make it take care of scrolling:
cp.add(new JScrollPane(tree), BorderLayout.CENTER);
// Capture the tree's model:
model =(DefaultTreeModel)tree.getModel();
JButton test = new JButton("Press me");
test.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(i < data.length) {
child = new Branch(data[i++]).node();
// What's the last one you clicked?
chosen = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if(chosen == null)
chosen = root;
// The model will create the appropriate event.
// In response, the tree will update itself:
model.insertNodeInto(child, chosen, 0);
// Puts the new node on the chosen node.
}
}
});
// Change the button's colors:
test.setBackground(Color.BLUE);
test.setForeground(Color.WHITE);
JPanel p = new JPanel();
p.add(test);
cp.add(p, BorderLayout.SOUTH);
}
public static void main(String[] args) {
Console.run(new Trees(), 250, 250);
}
} ///:~
</B></PRE></FONT>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -