LoadTree() 2 { jTabbedPane1.setCloseButtonEnabled(true); 5 6 DefaultMutableTreeNode node1 = new DefaultMutableTreeNode("软件部"); 7 node1.add(new DefaultMutableTreeNode("产品部")); 8 node1.add(new DefaultMutableTreeNode("测试部")); 9 node1.add(new DefaultMutableTreeNode("设计部")); 10 11 DefaultMutableTreeNode node2 = new DefaultMutableTreeNode("销售部"); 12 node2.add(new DefaultMutableTreeNode("jack")); 13 node2.add(new DefaultMutableTreeNode("Lily")); 14 node2.add(new DefaultMutableTreeNode("Smith")); 15 16 DefaultMutableTreeNode top = new DefaultMutableTreeNode("职员管理"); top.add(new DefaultMutableTreeNode("总经理")); 20 top.add(node1); 21 top.add(node2); DefaultTreeModel model = new DefaultTreeModel (top); 25 this.jTree1.setModel(model); 26 //jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION) { 31 Image icon= ImageIO.read(this.getClass().getResource("/images/Icon.png")); 32 tabIcon = createImageIcon("/images/Icon.png", "tab icon"); .setIconImage(icon); 35 } 36 catch(IOException ex) 37 { 38 39 System.out.println(ex); 40 41 } 42 43 }
在Tree的值变化事件中,通过class.forName()和 cls.newInstance()反射动态实例化窗体,代码如下:
jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
jTree1.getLastSelectedPathComponent();
(node == null){
;
9
}
10
11
Object nodeInfo = node.getUserObject();
12
String item = (String) nodeInfo;
(node.isLeaf()) {
15
String item1 = (String) nodeInfo;
16
// this.setTitle(item1);
17
//File f = new File("client.jar");
18
//URLClassLoader cl = new URLClassLoader(new URL[]{f.toURI().toURL(), null});
19
//Class<?> clazz = cl.loadClass("epicurus.Client");
20
//Method main = clazz.getMethod("main", String[].class);
{
23
Class cls = Class.forName("com.mkmis.forms.JIFrame1");
24
javax.swing.JInternalFrame frm =
(javax.swing.JInternalFrame) cls.newInstance();
25
frm.setVisible(true);
jTabbedPane1.addTab(" "+item1+" ",this.tabIcon,frm);
29
30
}
31
catch (Throwable e) {
32
System.err.println(e);
33
}
34
} else {
35
System.out.println("not leaf");
36
}
37
}
在javaswing中的路径也和net不同,下面定义了一个创建ImageIcon的方法:
ImageIcon createImageIcon(String path,String description) { 3 java.net.URL imgURL = getClass().getResource(path); 4 if (imgURL != null) { ImageIcon(imgURL, description); 6 } else { 7 System.err.println("Couldn't find file: " + path); ; 9 } 10 }
5 JDBC MYSQL代码