当前位置: 技术问答>java相关
java怎样实现树?
来源: 互联网 发布时间:2015-06-30
本文导语: 你好,请问在applet里面java怎样实现树状菜单,请高手赐教! | 给你个最简单的例子: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class TreeDemo1 { public TreeD...
你好,请问在applet里面java怎样实现树状菜单,请高手赐教!
|
给你个最简单的例子:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TreeDemo1
{
public TreeDemo1()
{
JFrame f = new JFrame("TreeDemo");
Container contentPane = f.getContentPane();
String[] s1 = {"公司文件","个人信件","私人文件"};
String[] s2 = {"本机磁盘(C:)","本机磁盘(D:)","本机磁盘(E:)"};
String[] s3 = {"奇摩站","职棒消息","网络书店"};
Hashtable hashtable1 = new Hashtable();
Hashtable hashtable2 = new Hashtable();
hashtable1.put("我的公文包",s1);
hashtable1.put("我的电脑",s2);
hashtable1.put("收藏夹",hashtable2);
hashtable2.put("网站列表",s3);
JTree tree = new JTree(hashtable1);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
contentPane.add(scrollPane);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
new TreeDemo1();
}
}
jb里有控件,在swing里
例子在jb目录下:sampleswingtreecontrol里
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TreeDemo1
{
public TreeDemo1()
{
JFrame f = new JFrame("TreeDemo");
Container contentPane = f.getContentPane();
String[] s1 = {"公司文件","个人信件","私人文件"};
String[] s2 = {"本机磁盘(C:)","本机磁盘(D:)","本机磁盘(E:)"};
String[] s3 = {"奇摩站","职棒消息","网络书店"};
Hashtable hashtable1 = new Hashtable();
Hashtable hashtable2 = new Hashtable();
hashtable1.put("我的公文包",s1);
hashtable1.put("我的电脑",s2);
hashtable1.put("收藏夹",hashtable2);
hashtable2.put("网站列表",s3);
JTree tree = new JTree(hashtable1);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
contentPane.add(scrollPane);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String args[]) {
new TreeDemo1();
}
}
jb里有控件,在swing里
例子在jb目录下:sampleswingtreecontrol里
|
自带控件!
|
java里的xml可以,好像要加一个xml的编译包