100分请教关于阿信的树形菜单! 用xml吧,很简单的,如果要的话我给你发过来 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.tree.*;import java.applet.*;import javax.swing.event.TreeModelListener;import netscape.javascript.*;import netscape.javascript.JSException ;public class Mytree extends JApplet { JTree tree = null; DefaultTreeModel treeModel = null; String nodeName = null; //原有节点名称 DefaultMutableTreeNode parentNode = null; TreePath parentPath =null; LinkedList All=new LinkedList(); LinkedList AllID=new LinkedList(); LinkedList AllName=new LinkedList(); public void init() { Container ctp=getContentPane(); DefaultMutableTreeNode root = new DefaultMutableTreeNode("部门列表"); parentNode=root; tree = new JTree(root); parentPath = new TreePath(root.getPath()); All.add(parentNode); AllID.add("-1"); AllName.add(""); tree.setEditable(false); tree.addMouseListener(new MouseHandle()); treeModel = (DefaultTreeModel)tree.getModel(); JScrollPane scrollPane = new JScrollPane(); scrollPane.setViewportView(tree); ctp.add(scrollPane,BorderLayout.CENTER); } class MouseHandle extends MouseAdapter { public void mousePressed(MouseEvent e) { if(e.getClickCount()==2) { try { JTree tree = (JTree)e.getSource(); int rowLocation = tree.getRowForLocation(e.getX(), e.getY()); TreePath treepath = tree.getPathForRow(rowLocation); TreeNode treenode = (TreeNode) treepath.getLastPathComponent(); int Index=FindLocate(treenode); MouseDoubleClick(Index); } catch(NullPointerException ne) { } } } } public void addFather(String IndexID,String IndexName,String ParentID) { DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(IndexName,true); All.add(newNode); AllID.add(IndexID); AllName.add(IndexName); if (ParentID.equals("0")) { parentNode=(DefaultMutableTreeNode)All.get(0); } else { parentNode=FindFatherNode(ParentID,IndexID); } if(parentNode!=null) { treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount()); tree.scrollPathToVisible(new TreePath(newNode.getPath())); } } public DefaultMutableTreeNode FindFatherNode(String ParentID,String IndexID) { DefaultMutableTreeNode ReturnRoot=null; for (int i=0;i<AllID.size() ;i++ ) { if(((String)AllID.get(i)).equals(ParentID)) { ReturnRoot=(DefaultMutableTreeNode)All.get(i); break; } } return ReturnRoot; } public void MouseDoubleClick(int Index) { JSObject win = JSObject.getWindow(this); if (Index>0&&Index<AllID.size()) { String ID=(String)AllID.get(Index); String IndexName=(String)AllName.get(Index); win.eval("showADep('"+ID+"')"); } } public int FindLocate(TreeNode treenode) { int Locate=-1; for (int i=0;i<All.size();i++) { if (((TreeNode)All.get(i)).equals(treenode)) { Locate=i; break; } } return Locate; } public void TreeClose() { int Count=tree.getRowCount(); for(int i=1;i<=Count;i++) { tree.collapseRow(i); } }} Reign001(afdf)没有这么复杂吧,那套系统使用的JAVASCRIPT <script language="javascript">//dreamweaver自带的函数// Example: obj = findObj("image1");function findObj(theObj, theDoc){ var p, i, foundObj; if(!theDoc) theDoc = document; if( (p = theObj.indexOf("?")) > 0 && parent.frames.length) { theDoc = parent.frames[theObj.substring(p+1)].document; theObj = theObj.substring(0,p); } if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj]; for (i=0; !foundObj && i < theDoc.forms.length; i++) foundObj = theDoc.forms[i][theObj]; for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) foundObj = findObj(theObj,theDoc.layers[i].document); if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj); return foundObj;}//点开菜单,同级菜单收缩。var oldObj='none'; 用来保存上次点开的菜单的idvar start=1; 程序开始标志var clicked=0; 菜单展开标志,默认全部关闭function controlMenu(theObj)//用来控制菜单的函数{ with(findObj(theObj).style) { display=display=='none'?'':'none'; }}function setMenu(theObj)//关键代码,默认值为菜单全部收缩。{ if(start) //第一次运行时start为真。展开菜单 { controlMenu(theObj); clicked=1; } else //start为假 { if(oldObj!=theObj) //点击的菜单跟上次不同 { //如果有菜单为展开状态,则将其关闭。 if(clicked)controlMenu(oldObj); //展开新菜单 controlMenu(theObj); //设置展开标志 clicked=1; } else //点击的菜单跟上次点击一样 { //收缩菜单 controlMenu(theObj); //设置展开标志为否 clicked=!clicked; } } start=0; //设置程序开始标志为否。 oldObj=theObj; //保存当前状态}</script>--------------------------------------------程序还有需要优化的地方。不过工作的很好。遗憾的是,只支持一级菜单。哪位高人修改一下?我认为多级菜单实在不需要收缩其他菜单的功能,所以当时没考虑。何况,还要判断是否在同一分支下,决定是否展开或收缩。麻烦啊~~ 怕怕 看这里了:演示:http://61.163.246.155/pro/XMLSelTree/Examples/Examples_01/default.htm演示:http://61.163.246.155/pro/XMLSelTree/Examples/Examples_02/default.htm下载:http://61.163.246.155/pro/XMLSelTree/或 http://61.163.246.155/pro/XMLSelTree.rar祝你好运! to:lawdoor(【风语者】)有没有 能新增修改的例子? 写着玩,支持多级和插入删除<head><title>MyTree Test Page</title></head><script language="Jscript">function ftree(name){ this.tbl = document.createElement("table"); this.tbl.name=name; this.tbl.onclick=this.check; this.tbl.oncontextmenu=this.showmenu; this.append(name,0,1);}var menu;var opobj;function createmenu(){ menu = document.createElement("div"); menu.style.position="absolute"; var a = document.createElement("a"); a.onclick=delrow; a.style.borderStyle="solid"; a.style.borderColor="black"; a.style.borderWidth=1; a.style.cursor="hand"; a.innerText="Delete"; a.style.color="red"; a.style.backgroundColor="#cccccc"; menu.appendChild(a); a = document.createElement("br"); menu.appendChild(a); a = document.createElement("a"); a.onclick=insertrow; a.style.borderStyle="solid"; a.style.borderColor="black"; a.style.borderWidth=1; a.style.cursor="hand"; a.innerText="Insert node"; a.style.backgroundColor="#cccccc"; a.style.color="red"; menu.appendChild(a); a = document.createElement("br"); menu.appendChild(a); a = document.createElement("a"); a.onclick=insertfolder; a.style.borderStyle="solid"; a.style.borderColor="black"; a.style.borderWidth=1; a.style.cursor="hand"; a.innerText="Insert folder"; a.style.backgroundColor="#cccccc"; a.style.color="red"; menu.appendChild(a); in1.appendChild(this.menu); menu.style.display ="none";// alert(menu.outerHTML);}function hidemenu(){ if(menu) menu.style.display ="none";}function delrow(){// alert(opobj);// alert(opobj.oprow); if(opobj && opobj.oprow) opobj.del(opobj.oprow); hidemenu(); }var fname="";var oDia="";function insertrow(){ if(opobj && opobj.tbl.rows(opobj.oprow).ftype==0)return; fname=prompt("Insert Name:","T11"); if(fname) if(opobj && opobj.oprow) opobj.insert(opobj.oprow+1,fname/*name*/,Number(opobj.tbl.rows(opobj.oprow).level)+1/*level*/,0/*type*/); hidemenu();}function insertfolder(){ if(opobj && opobj.tbl.rows(opobj.oprow).ftype==0)return; fname=prompt("Insert Folder:","T10"); if(fname) if(opobj && opobj.oprow) opobj.insert(opobj.oprow+1,fname/*name*/,Number(opobj.tbl.rows(opobj.oprow).level)+1/*level*/,1/*type*/); hidemenu();}ftree.prototype.showmenu=function(){ var o = window.event.srcElement; if(o==null)return; while(o.tagName != "TD" && o.tagName != "TH") { o = o.parentElement; if(o==null)return; } var b = o.parentElement.rowIndex; opobj=eval(this.name); opobj.oprow=b; if(menu==null)createmenu(); menu.style.display ="inline"; menu.style.left=window.event.x; menu.style.top=window.event.y; return false;}ftree.prototype.append=function(name,level,ftype){ this.insert(this.tbl.rows.length,name,level,ftype);}ftree.prototype.check=function(){ var o = window.event.srcElement; if(o==null)return; while(o.tagName != "TD" && o.tagName != "TH") { o = o.parentElement; if(o==null)return; } var b = o.parentElement.rowIndex; var obj = eval(this.name); obj.show(b,!this.rows[b].cshow,0);}ftree.prototype.show=function(index,bshow,bshowchild){ if(index>=this.tbl.rows.length)return; var tnode=this.tbl.rows[index]; tnode.cshow=bshow; var i=1; while(1) { if(index+i>=this.tbl.rows.length)return; var nnode = this.tbl.rows[index+i]; if(nnode.level<=tnode.level)return; if(nnode.level-tnode.level==1) { this.tbl.rows[index+i].style.display=bshow?"inline":"none"; if(!bshow || bshowchild) { this.show(index+i,bshow,bshowchild); } } i+=1; }}ftree.prototype.del=function(index){ if(index==null)index=this.oprow; if(index==null)return; if(index>=this.tbl.rows.length)return; var tnode=this.tbl.rows[index]; var i=1; while(1) { if(index+i>=this.tbl.rows.length)break; var nnode = this.tbl.rows[index+i]; if(nnode.level<=tnode.level)break; if(nnode.level-tnode.level==1) { { this.del(index+i); } } i+=1; } this.tbl.deleteRow(index);}ftree.prototype.insert=function(index,name,level,ftype){ var r =this.tbl.insertRow(index); r.level=level; r.ftype=ftype; r.cshow=true; var c =r.insertCell(); var a =document.createElement("a"); a.innerText=name; if(ftype) { a.style.backgroundColor="#999999"; a.style.borderStyle="solid"; a.style.borderColor="black"; a.style.borderWidth=1; a.style.cursor="hand"; } a.style.marginLeft=level*12; c.appendChild(a); }var tree1;function init(){ tree1 = new ftree("tree1"); tree1.append("t1",1,1); tree1.append("t2",2,1); tree1.append("t3",3,0); tree1.append("t4",1,1); tree1.append("t5",2,1); tree1.append("t6",2,0); tree1.append("t7",2,0); tree1.append("t8",2,0); tree1.append("t9",2,1); tree1.append("t10",3,1); tree1.append("t11",4,0); tree1.append("t12",4,0); in1.appendChild(tree1.tbl);}</script><body onload="init()" onclick="hidemenu()"> <span id="in1"> <FONT face="宋体"></FONT> </span></body></html> 关于JavaScript截取input赋值 svg 中 关于contextMenu这个右键菜单 类 帮我解释下这段代码 javascript如何在类中使用定时器? javascript 缺少对象 错误 奇怪! 有什么方法可以在Textarea指定位置地方插入图片呢 在网页中插入文字的问题 如何利用javascript保持状态? mozilla下如何判断用户按键的码值 求救!在线等,关于密码验证 请教多表单提交问题 如何返回年月日?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.applet.*;
import javax.swing.event.TreeModelListener;
import netscape.javascript.*;
import netscape.javascript.JSException ;
public class Mytree extends JApplet
{
JTree tree = null;
DefaultTreeModel treeModel = null;
String nodeName = null; //原有节点名称
DefaultMutableTreeNode parentNode = null;
TreePath parentPath =null;
LinkedList All=new LinkedList();
LinkedList AllID=new LinkedList();
LinkedList AllName=new LinkedList();
public void init()
{
Container ctp=getContentPane();
DefaultMutableTreeNode root = new DefaultMutableTreeNode("部门列表");
parentNode=root;
tree = new JTree(root);
parentPath = new TreePath(root.getPath());
All.add(parentNode);
AllID.add("-1");
AllName.add("");
tree.setEditable(false);
tree.addMouseListener(new MouseHandle());
treeModel = (DefaultTreeModel)tree.getModel();
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewportView(tree);
ctp.add(scrollPane,BorderLayout.CENTER);
} class MouseHandle extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
if(e.getClickCount()==2)
{
try
{
JTree tree = (JTree)e.getSource();
int rowLocation = tree.getRowForLocation(e.getX(), e.getY());
TreePath treepath = tree.getPathForRow(rowLocation);
TreeNode treenode = (TreeNode) treepath.getLastPathComponent();
int Index=FindLocate(treenode);
MouseDoubleClick(Index);
}
catch(NullPointerException ne)
{
}
}
}
}
public void addFather(String IndexID,String IndexName,String ParentID)
{
DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(IndexName,true);
All.add(newNode);
AllID.add(IndexID);
AllName.add(IndexName);
if (ParentID.equals("0"))
{
parentNode=(DefaultMutableTreeNode)All.get(0);
}
else
{
parentNode=FindFatherNode(ParentID,IndexID);
}
if(parentNode!=null)
{
treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount());
tree.scrollPathToVisible(new TreePath(newNode.getPath()));
}
}
public DefaultMutableTreeNode FindFatherNode(String ParentID,String IndexID)
{
DefaultMutableTreeNode ReturnRoot=null;
for (int i=0;i<AllID.size() ;i++ )
{
if(((String)AllID.get(i)).equals(ParentID))
{
ReturnRoot=(DefaultMutableTreeNode)All.get(i);
break;
}
}
return ReturnRoot;
}
public void MouseDoubleClick(int Index)
{
JSObject win = JSObject.getWindow(this);
if (Index>0&&Index<AllID.size())
{
String ID=(String)AllID.get(Index);
String IndexName=(String)AllName.get(Index);
win.eval("showADep('"+ID+"')");
}
}
public int FindLocate(TreeNode treenode)
{
int Locate=-1;
for (int i=0;i<All.size();i++)
{
if (((TreeNode)All.get(i)).equals(treenode))
{
Locate=i;
break;
}
}
return Locate;
}
public void TreeClose()
{
int Count=tree.getRowCount();
for(int i=1;i<=Count;i++)
{
tree.collapseRow(i);
}
}}
//dreamweaver自带的函数
// Example: obj = findObj("image1");
function findObj(theObj, theDoc)
{
var p, i, foundObj;
if(!theDoc) theDoc = document;
if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
{
theDoc = parent.frames[theObj.substring(p+1)].document;
theObj = theObj.substring(0,p);
}
if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
for (i=0; !foundObj && i < theDoc.forms.length; i++)
foundObj = theDoc.forms[i][theObj];
for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
foundObj = findObj(theObj,theDoc.layers[i].document);
if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
return foundObj;
}//点开菜单,同级菜单收缩。var oldObj='none'; 用来保存上次点开的菜单的id
var start=1; 程序开始标志
var clicked=0; 菜单展开标志,默认全部关闭
function controlMenu(theObj)
//用来控制菜单的函数
{
with(findObj(theObj).style)
{
display=display=='none'?'':'none';
}
}
function setMenu(theObj)
//关键代码,默认值为菜单全部收缩。
{
if(start)
//第一次运行时start为真。展开菜单
{
controlMenu(theObj);
clicked=1;
}
else
//start为假
{
if(oldObj!=theObj)
//点击的菜单跟上次不同
{
//如果有菜单为展开状态,则将其关闭。
if(clicked)controlMenu(oldObj);
//展开新菜单
controlMenu(theObj);
//设置展开标志
clicked=1;
}
else
//点击的菜单跟上次点击一样
{
//收缩菜单
controlMenu(theObj);
//设置展开标志为否
clicked=!clicked;
}
}
start=0; //设置程序开始标志为否。
oldObj=theObj; //保存当前状态
}
</script>
--------------------------------------------
程序还有需要优化的地方。不过工作的很好。遗憾的是,只支持一级菜单。哪位高人修改一下?我认为多级菜单实在不需要收缩其他菜单的功能,所以当时没考虑。何况,还要判断是否在同一分支下,决定是否展开或收缩。麻烦啊~~ 怕怕
演示:http://61.163.246.155/pro/XMLSelTree/Examples/Examples_01/default.htm
演示:http://61.163.246.155/pro/XMLSelTree/Examples/Examples_02/default.htm
下载:http://61.163.246.155/pro/XMLSelTree/或
http://61.163.246.155/pro/XMLSelTree.rar
祝你好运!
有没有 能新增修改的例子?
<head>
<title>MyTree Test Page</title>
</head><script language="Jscript">
function ftree(name)
{
this.tbl = document.createElement("table");
this.tbl.name=name;
this.tbl.onclick=this.check;
this.tbl.oncontextmenu=this.showmenu;
this.append(name,0,1);
}
var menu;
var opobj;
function createmenu()
{
menu = document.createElement("div");
menu.style.position="absolute";
var a = document.createElement("a");
a.onclick=delrow;
a.style.borderStyle="solid";
a.style.borderColor="black";
a.style.borderWidth=1;
a.style.cursor="hand";
a.innerText="Delete";
a.style.color="red";
a.style.backgroundColor="#cccccc";
menu.appendChild(a);
a = document.createElement("br");
menu.appendChild(a);
a = document.createElement("a");
a.onclick=insertrow;
a.style.borderStyle="solid";
a.style.borderColor="black";
a.style.borderWidth=1;
a.style.cursor="hand";
a.innerText="Insert node";
a.style.backgroundColor="#cccccc";
a.style.color="red";
menu.appendChild(a);
a = document.createElement("br");
menu.appendChild(a);
a = document.createElement("a");
a.onclick=insertfolder;
a.style.borderStyle="solid";
a.style.borderColor="black";
a.style.borderWidth=1;
a.style.cursor="hand";
a.innerText="Insert folder";
a.style.backgroundColor="#cccccc";
a.style.color="red";
menu.appendChild(a);
in1.appendChild(this.menu);
menu.style.display ="none";
// alert(menu.outerHTML);
}function hidemenu()
{
if(menu)
menu.style.display ="none";
}function delrow()
{
// alert(opobj);
// alert(opobj.oprow);
if(opobj && opobj.oprow)
opobj.del(opobj.oprow);
hidemenu();
}
var fname="";
var oDia="";
function insertrow()
{
if(opobj && opobj.tbl.rows(opobj.oprow).ftype==0)return;
fname=prompt("Insert Name:","T11");
if(fname)
if(opobj && opobj.oprow)
opobj.insert(opobj.oprow+1,fname/*name*/,Number(opobj.tbl.rows(opobj.oprow).level)+1/*level*/,0/*type*/);
hidemenu();
}function insertfolder()
{
if(opobj && opobj.tbl.rows(opobj.oprow).ftype==0)return;
fname=prompt("Insert Folder:","T10");
if(fname)
if(opobj && opobj.oprow)
opobj.insert(opobj.oprow+1,fname/*name*/,Number(opobj.tbl.rows(opobj.oprow).level)+1/*level*/,1/*type*/);
hidemenu();
}ftree.prototype.showmenu=function()
{
var o = window.event.srcElement;
if(o==null)return;
while(o.tagName != "TD" && o.tagName != "TH")
{
o = o.parentElement;
if(o==null)return;
}
var b = o.parentElement.rowIndex;
opobj=eval(this.name);
opobj.oprow=b;
if(menu==null)createmenu();
menu.style.display ="inline";
menu.style.left=window.event.x;
menu.style.top=window.event.y;
return false;
}ftree.prototype.append=function(name,level,ftype)
{
this.insert(this.tbl.rows.length,name,level,ftype);
}
ftree.prototype.check=function()
{
var o = window.event.srcElement;
if(o==null)return;
while(o.tagName != "TD" && o.tagName != "TH")
{
o = o.parentElement;
if(o==null)return;
}
var b = o.parentElement.rowIndex;
var obj = eval(this.name);
obj.show(b,!this.rows[b].cshow,0);
}ftree.prototype.show=function(index,bshow,bshowchild)
{
if(index>=this.tbl.rows.length)return;
var tnode=this.tbl.rows[index];
tnode.cshow=bshow;
var i=1;
while(1)
{
if(index+i>=this.tbl.rows.length)return;
var nnode = this.tbl.rows[index+i];
if(nnode.level<=tnode.level)return;
if(nnode.level-tnode.level==1)
{
this.tbl.rows[index+i].style.display=bshow?"inline":"none";
if(!bshow || bshowchild)
{
this.show(index+i,bshow,bshowchild);
}
}
i+=1;
}
}ftree.prototype.del=function(index)
{
if(index==null)index=this.oprow;
if(index==null)return;
if(index>=this.tbl.rows.length)return;
var tnode=this.tbl.rows[index];
var i=1;
while(1)
{
if(index+i>=this.tbl.rows.length)break;
var nnode = this.tbl.rows[index+i];
if(nnode.level<=tnode.level)break;
if(nnode.level-tnode.level==1)
{
{
this.del(index+i);
}
}
i+=1;
}
this.tbl.deleteRow(index);
}ftree.prototype.insert=function(index,name,level,ftype)
{
var r =this.tbl.insertRow(index);
r.level=level;
r.ftype=ftype;
r.cshow=true;
var c =r.insertCell();
var a =document.createElement("a");
a.innerText=name;
if(ftype)
{
a.style.backgroundColor="#999999";
a.style.borderStyle="solid";
a.style.borderColor="black";
a.style.borderWidth=1;
a.style.cursor="hand";
}
a.style.marginLeft=level*12;
c.appendChild(a);
}var tree1;function init()
{
tree1 = new ftree("tree1");
tree1.append("t1",1,1);
tree1.append("t2",2,1);
tree1.append("t3",3,0);
tree1.append("t4",1,1);
tree1.append("t5",2,1);
tree1.append("t6",2,0);
tree1.append("t7",2,0);
tree1.append("t8",2,0);
tree1.append("t9",2,1);
tree1.append("t10",3,1);
tree1.append("t11",4,0);
tree1.append("t12",4,0);
in1.appendChild(tree1.tbl);
}</script>
<body onload="init()" onclick="hidemenu()">
<span id="in1">
<FONT face="宋体"></FONT>
</span>
</body>
</html>