We often use tree menus when making web pages, and mostly write scripts. This certainly has its advantages of flexibility and diversity, but it is relatively complicated to do. Why don't we use the powerful functions of Java swing for our web services? So I will discuss with you how to use the powerful functions of Applet to create a tree menu. The Code is as follows:
Package application;
Import java. AWT .*;
Import java. AWT. event .*;
Import java. Io .*;
Import java. util .*;
Import java.net .*;
Import java. Applet .*;
Import javax. Swing .*;
Import javax. Swing. Tree .*;
Import javax. Swing. event .*;
Public class treeapplet extends javax. Swing. japplet {
/** Initializes the applet treeapplet */
Public void Init (){
Try {
M_model = initmanumodel ();
Java. AWT. eventqueue. invokeandwait (New runnable (){
Public void run (){
Initcomponents ();
}
});
} Catch (exception ex ){
Ex. printstacktrace ();
}
}
Private void initcomponents (){
Jscrollpane = new javax. Swing. jscrollpane ();
Jtree = new javax. Swing. jtree ();
Jscrollpane. setviewportborder (New javax. swing. border. etchedborder (javax. swing. border. etchedborder. raised, new Java. AWT. color (204, 0,204), new Java. AWT. color (51, 51, 51 )));
Jtree. setdragenabled (true );
Jtree. setmodel (m_model );
Jtree. addmousemotionlistener (New java. AWT. event. mousemotionadapter (){
Public void mousedragged (Java. AWT. event. mouseevent EVT ){
Jtreemousedragged (EVT );
}
});
Jtree. addmouselistener (New java. AWT. event. mouseadapter (){
Public void mouseclicked (Java. AWT. event. mouseevent EVT ){
Jtreemouseclicked (EVT );
}
});
Jscrollpane. setviewportview (jtree );
Getcontentpane (). Add (jscrollpane, java. AWT. borderlayout. center );
}
Private void jtreemouseclicked (Java. AWT. event. mouseevent EVT) {// gen-First: event_jtreemouseclicked
Int selrow = jtree. getrowforlocation (EVT. getx (), EVT. Gety ());
Javax. Swing. Tree. treepath selpath = jtree. getpathforlocation (EVT. getx (), EVT. Gety ());
If (selrow! =-1)
{
Javax. Swing. Tree. defaultmutabletreenode node =
(Defaultmutabletreenode) selpath. getlastpathcomponent ();
Manunode imanunode = (manunode) node. getuserobject ();
System. Out. println (imanunode. tostring ());
If (imanunode. GetType () = 1 ){
If (EVT. getclickcount () = 2 ){
Try {
URL url = new URL (imanunode. geturl ());
Appletcontext context = getappletcontext ();
If (! Imanunode. gettarge (). Equals ("")){
Context. showdocument (URL, imanunode. gettarge ());
} Else {
Context. showdocument (URL, "_ blank ");
}
}
Catch (malformedurlexception e ){
System. Out. println (E );
}
}
}
}
}
Private javax. Swing. Tree. defaulttreemodel initmanumodel (){
Javax. Swing. Tree. defaulttreemodel m_model;
Javax. Swing. Tree. defaultmutabletreenode top;
Loop = 0;
Top = initnode (0 );
If (Top = NULL ){
Top = new javax. Swing. Tree. defaultmutabletreenode (New manunode (-1, "error !!! ","",""));
}
M_model = new javax. Swing. Tree. defaulttreemodel (top );
Return m_model;
}
Private javax. Swing. Tree. defaultmutabletreenode initnode (INT parenid ){
Javax. Swing. Tree. defaultmutabletreenode node, Childe;
If (parenid =-1)
Return NULL;
Manunode imanunode;
Imanunode = getmanunode ("Param" + parenid );
Node = new javax. Swing. Tree. defaultmutabletreenode (imanunode );
While (true ){
Loop ++;
Imanunode = getmanunode ("Param" + loop );
If (imanunode. GetType () = 1 ){
Childe = new javax. Swing. Tree. defaultmutabletreenode (imanunode );
Node. Add (Childe );
} Else if (imanunode. GetType () = 0 ){
Childe = initnode (loop );
Node. Add (Childe );
} If (imanunode. GetType () = 2 ){
Break;
}
}
Return node;
}
Private manunode getmanunode (string name ){
String retstring = getparameter (name );
Manunode imanunode;
String stype = "";
Int itype =-1;
String manuname = "";
String url = "";
String targer = "";
Stringtokenizer stoken;
If (retstring! = NULL ){
Token en = new stringtokenizer (retstring ,",");
If (stoken. hasmoretokens ())
Stype = token en. nexttoken ();
If (stoken. hasmoretokens ())
Manuname = EN en. nexttoken ();
If (stoken. hasmoretokens ())
Url = EN en. nexttoken ();
If (stoken. hasmoretokens ())
Targer = token en. nexttoken ();
If (! Stype. Equals (""))
Itype = integer. parseint (stype );
}
Imanunode = new manunode (itype, manuname, URL, targer );
Return imanunode;
}
Private class manunode extends object {
Public manunode (){
This. type =-1;
This. manuname = "";
This. url = "";
This. targe = "";
}
Public manunode (INT type, string manuname ){
This. type = type;
This. manuname = manuname;
This. url = "";
This. targe = "";
}
Public manunode (INT type, string manuname, string URL ){
This. type = type;
This. manuname = manuname;
This. url = URL;
This. targe = "";
}
Public manunode (INT type, string manuname, string URL, string targe ){
This. type = type;
This. manuname = manuname;
This. url = URL;
This. targe = targe;
}
Public int GetType (){
Return type;
}
Public String getmanuname (){
Return manuname;
}
Public String geturl (){
Return URL;
}
Public String gettarge (){
Return targe;
}
Public void settype (INT type ){
This. type = type;
}
Public void setmanuname (string manuname ){
This. manuname = manuname;
}
Public void seturl (string URL ){
This. url = URL;
}
Public void settarge (string targe ){
This. targe = targe;
}
Public String tostring (){
Return manuname;
}
Private int type;
Private string manuname;
Private string URL;
Private string targe;
}
Private javax. Swing. Tree. defaulttreemodel m_model;
Int loop;
Private javax. Swing. jscrollpane;
Private javax. Swing. jtree;
}
After completing these steps, let's talk about passing parameters. The following is an example of my Configuration:
<APPLET codebase = "" code = "application/treeapplet. Class" width = 200 Height = 300>
<Param name = "param0" value = "0, Manu,">
<Param name = "param1" value = "0, manuitem1, URL, targe">
<Param name = "param2" value = "1, manuitem1, http://www.baidu.com, _ Self">
<Param name = "param3" value = "2, manuitem1, URL, targe">
<Param name = "param4" value = "0, manuitem2, URL, targe">
<Param name = "param5" value = "1, manuitem1, http://www.baidu.com, _ top">
<Param name = "param6" value = "2, manuitem2, URL, targe">
<Param name = "param7" value = "1, manuitem3, http://www.baidu.com,">
<Param name = "param8" value = "2, Manu,">
</APPLET>
Where value = "type, manuitemname, URL, targe", type value 0, 1, 2; 0 is the start of a menu, 1 indicates that this item is a menu item, then URL, and targe are valid, 2 indicates the end of the menu. Can be nested for use, just like the above example. This menu is very simple and has many incomplete functions. You can use this as the basis to expand more functions.