⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 duwenfengtreemenu.java

📁 用Java Applet实现的树型导航控件
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.border.*;
import java.util.*;
import javax.swing.event.*;



/**
 * Title:        duwenfeng'JTREEMENU
 * Description:  use JTree to make a internet Menu
 * we can get the  parameter form the html
 * Copyright:    Copyright (c) 2001
 * Company:      csu
 * @author duwenfeng
 * @version 1.0
 */

public class DUWENFENGTREEMENU extends JApplet{
  boolean isStandalone = false;

//add a HashMap to store the name and URL of the tree
  Map TreeURLStringMap=new HashMap();
//define the root
  DefaultMutableTreeNode root=new DefaultMutableTreeNode("人工智能");
  JLabel jLabel1 = new JLabel();
  JLabel jLabel2 = new JLabel();
  JTextField LookUpTextField = new JTextField();
  JButton LookUpButton = new JButton();
  JScrollPane jScrollPane1 = new JScrollPane();
  JTree LookUpTree=new JTree(root);
  JButton PlayButton = new JButton();

  JButton StopButton = new JButton();
  TitledBorder titledBorder1;
  JProgressBar musicProgressBar = new JProgressBar();
  AudioClip music;


  /**Get a parameter value*/
  public String getParameter(String key, String def) {
    return isStandalone ? System.getProperty(key, def) :
      (getParameter(key) != null ? getParameter(key) : def);
  }

  /**Construct the applet*/
  public DUWENFENGTREEMENU() {
  }
  /**Initialize the applet*/
  public void init() {
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  /**Component initialization*/
  private void jbInit() throws Exception {
    titledBorder1 = new TitledBorder("");
    LookUpTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener()
    {
      public void valueChanged(TreeSelectionEvent e)
      {
        LookUpTree_valueChanged(e);
      }
    });

    this.getContentPane().setLayout(null);


    music=getAudioClip(getCodeBase(),"spacemusic.au");
   //  music.loop();

    //add the tree with the parameter
    String temp;

    temp=getParameter("Owner","NO");
    //System.out.print(tempOwner);
  if(temp.equals("duwenfeng"))

  {
    DefaultMutableTreeNode tempzhangnode;
    DefaultMutableTreeNode tempjienode;
    DefaultMutableTreeNode tempzhishidiannode;


    //make the root node


    //get the number of zhan
    temp=getParameter("zhangNum","0");
    int zhangnum=Integer.parseInt(temp);
    //define zhani to control the loop of zhan
    int zhangi;
    //define jiei to control the loop of jie
    int jiei;
     //define zhishidiani to control the loop of zhishidian
    int zhishidiani;
    //define zhantemp to store the name of zhan
    String zhangtemp[]=new String[zhangnum];
    //define jietemp to store the name of jie
    String jietemp[];
    //define zhishidiantemp to store the name of zhishidian
    String zhishidiantemp[];
    //define zhishidiantemp to store the name of zhishidian
    String zhishidian[];
    //define jienum to store the number of jie of all zhang
    int jienum[]=new int[zhangnum];
    //define zhishidiannum to store the number of zhishidian of all jie
    int zhishidiannum[];
    //use the treeURL class to get the name and the URL
    treeURL zhangtreeURL;
    treeURL jietreeURL;
    treeURL zhishidiantreeURL;
     //add all father node
    for(zhangi=1;zhangi<=zhangnum;zhangi++)
    {
      //get the name of all zhan
      zhangtemp[zhangi-1]=getParameter("zhang"+zhangi,"没有相关内容");
      zhangtreeURL=new treeURL(zhangtemp[zhangi-1]);
      tempzhangnode=new DefaultMutableTreeNode(zhangtreeURL.getname());
      /////////////////////////////////////add to TreeURLStringMap
      TreeURLStringMap.put(zhangtreeURL.getname(),zhangtreeURL.getURLAddress());
      //////////////////////////////////
      root.add(tempzhangnode);

//////////////////////////////////

       //得到各章的节数
      temp=getParameter("zhang"+zhangi+"jieNum","0");
      jienum[zhangi-1]=Integer.parseInt(temp);
      jietemp=new String[jienum[zhangi-1]];

      for(jiei=1;jiei<=jienum[zhangi-1];jiei++)
      {
        //add all node of jie
        jietemp[jiei-1]=getParameter("zhang"+zhangi+"jie"+jiei,"没有相关内容");
        //System.out.println(jietemp[jiei-1]);
        jietreeURL=new treeURL(jietemp[jiei-1]);

        tempjienode=new DefaultMutableTreeNode(jietreeURL.getname());
        ////////////////////////////add to TreeURLStringMap
        TreeURLStringMap.put(jietreeURL.getname(),jietreeURL.getURLAddress());
        /////////////////////////////////////////////////////
        tempzhangnode.add(tempjienode);

       ///得到各节知识点
       zhishidiannum=new int[jienum[zhangi-1]];
        temp=getParameter("zhang"+zhangi+"jie"+jiei+"zhishidianNum","0");
        zhishidiannum[jiei-1]=Integer.parseInt(temp);
        zhishidiantemp=new String[zhishidiannum[jiei-1]];

        for(zhishidiani=1;zhishidiani<=zhishidiannum[jiei-1];zhishidiani++)
        {
          zhishidiantemp[zhishidiani-1]=getParameter("zhang"+zhangi+"jie"+jiei+"zhishidian"+zhishidiani,"没有相关内容");
          zhishidiantreeURL=new treeURL(zhishidiantemp[zhishidiani-1]);
          tempzhishidiannode=new DefaultMutableTreeNode(zhishidiantreeURL.getname());
          ////////////////////////////////////add to TreeURLStringMap
          TreeURLStringMap.put(zhishidiantreeURL.getname(),zhishidiantreeURL.getURLAddress());

          //////////////////////////////

          tempjienode.add(tempzhishidiannode);
        }

      }
///////////////////////

     }

     ///////////////////////////////



///////////////////////////////////////////////////////////////////

 /*
      temp=getParameter("jieNum","0");
    int zhannum=Integer.parseInt(temp);
    //define zhani to control the loop of zhan
    int zhani;
    //define zhantemp to store the name of zhan
    String zhantemp[]=new String[zhannum];

    for(zhani=1;zhani<=zhannum;zhani++)
    {
     zhantemp[zhani-1]=getParameter("zhan"+zhani,"没有相关内容");
      //add all father node
      tempnode=new DefaultMutableTreeNode(zhantemp[zhani-1]);
      root.add(tempnode);
     }


  ////////////////////////////
*/

    //    String tempch;
    //    String childtemp[];

    //    temp=getParameter("childNum1","0");
    //    int num=Integer.parseInt(temp);
    //    childtemp=new String[num];
    //    int i;
    //    for(i=1;i<=num;i++)
    //    {
    //      tempch=getParameter("child1"+i,,"没有相关内容");
    //      DefaultMutableTreeNode fristCh1=new DefaultMutableTreeNode(tempch);
    //      fristCh.add(fristCh1);
    //    }
    //}






//////////////////////////////////////////////////////////////////////




    //LookUpTree=new JTree(root);
    LookUpTree.putClientProperty("JTree.lineStyle","v1000Angle");
    LookUpTree.setShowsRootHandles(true);
    LookUpTree.setBorder(BorderFactory.createLineBorder(Color.black));
    LookUpTree.setBounds(new Rectangle(1, 84, 185, 306));
    //add a TreeSelectionListener


    //////////////////
    jLabel1.setText("知识结构树:");
    jLabel1.setBounds(new Rectangle(8, 67, 80, 21));
    jLabel2.setText("知识点查询:");
    jLabel2.setBounds(new Rectangle(8, 9, 84, 21));
    LookUpTextField.setBorder(BorderFactory.createLineBorder(Color.black));
    LookUpTextField.setText("杜文峰");
    LookUpTextField.setBounds(new Rectangle(2, 31, 116, 24));
    LookUpButton.setBorder(BorderFactory.createLineBorder(Color.black));
    LookUpButton.setText("查询");
    LookUpButton.setBounds(new Rectangle(123, 30, 63, 24));

    jScrollPane1.setBounds(new Rectangle(4, 86, 182, 308));
    PlayButton.setBorder(BorderFactory.createLineBorder(Color.black));
    PlayButton.setToolTipText("Play");
    PlayButton.setActionCommand("PlayButton");
    PlayButton.setText("Play");
    PlayButton.setBounds(new Rectangle(20, 426, 39, 25));
    PlayButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        PlayButton_actionPerformed(e);
      }
    });
    StopButton.setBorder(BorderFactory.createLineBorder(Color.black));
    StopButton.setToolTipText("Stop");
    StopButton.setActionCommand("StopButton");
    StopButton.setText("Stop");
    StopButton.setBounds(new Rectangle(129, 427, 39, 25));
    StopButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        StopButton_actionPerformed(e);
      }
    });
    musicProgressBar.setBorder(BorderFactory.createEtchedBorder());
    musicProgressBar.setBounds(new Rectangle(5, 406, 182, 13));
    this.getContentPane().add(LookUpButton, null);
    this.getContentPane().add(jLabel1, null);
    this.getContentPane().add(LookUpTextField, null);
    this.getContentPane().add(jScrollPane1, null);
    this.getContentPane().add(jLabel2, null);
    jScrollPane1.getViewport().add(LookUpTree, null);
    this.getContentPane().add(musicProgressBar, null);
    this.getContentPane().add(StopButton, null);
    this.getContentPane().add(PlayButton, null);

   ///// this.getContentPane().add(LookUpTree, null);

}

  }
  /**Get Applet information*/
  public String getAppletInfo() {
    return   "duwenfeng's TreeMenu";
      }
  /**Get parameter info*/
  public String[][] getParameterInfo() {
    return null;
  }

  //static initializer for setting look & feel
  static {
    try {
      //UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
    }
    catch(Exception e) {
    }
  }

  void PlayButton_actionPerformed(ActionEvent e)
  {

    // System.out.println(TreeURLStringMap.get("第4章第4节知识点1"));
      music.loop();
  }

  void StopButton_actionPerformed(ActionEvent e)
  {
    music.stop();
  }

  void LookUpTree_valueChanged(TreeSelectionEvent e)
  {
     TreePath path=LookUpTree.getSelectionPath();
     if(path==null)
       return;
     DefaultMutableTreeNode selectedNode=(DefaultMutableTreeNode)path.getLastPathComponent();
     //Class c=(Class)selectedNode.getUserObject();
     //String description=getFieldDescription(c);
    // System.out.println(description);
    String URLname=selectedNode.toString();
    System.out.println(URLname);
    System.out.println(TreeURLStringMap.get(URLname));
  }



}

class treeURL{
  public treeURL(String s)
  {
      int commaindex=s.indexOf(",");
       name=s.substring(0,commaindex);
       URLString=s.substring(commaindex+1,s.length());
       //check the result of the String  and show the name and email address
       //System.out.println(s);
       //System.out.println(name);
       //System.out.println(URLString);
  }

  public String getname()
  {
    return name;
  }

  public String getURLAddress()
  {
    return URLString;
  }

String name;
String URLString="待加入网址";


//end of the class treeURL
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -