用APPLET来写树形菜单

我们在做网页的时候常什么用到树形菜单,大多用脚本编写。这样固然有它灵活多样的好处,但是做起来相对复杂。我们不为什么不借助于JAVA SWING的强大功能来做我们的WEB服务呢?于是这里和大家一起讨论一下利用APPLET的强大功能来做个树形菜单。代码如下:

 

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){
            sToken=new StringTokenizer(retString,",");
            if(sToken.hasMoreTokens())
                sType=sToken.nextToken();
            if(sToken.hasMoreTokens())
                ManuName=sToken.nextToken();
            if(sToken.hasMoreTokens())
                Url=sToken.nextToken();
             if(sToken.hasMoreTokens())
                Targer=sToken.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 jScrollPane;
    private javax.swing.JTree jTree;
   
   
}
我完成这些后,说一下参数的传递。下面是我配的一个示例:

<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>
其中VALUE="Type,ManuItemName,url,targe ",Type取值0,1,2;0是一个菜单的开始,1表示该项是菜单项则url,和Targe有效,2则表示菜单的结束。可嵌套使用,使用时仿照上面的示例即可。这个菜单做的很简单,有很多功能不完善,大家可已此为基础,扩展更多功能。

 

 

你可能感兴趣的:(用APPLET来写树形菜单)