YCOE

You Can't stOp mE!

导航

简易标准JavaBean部分代码生成器

Posted on 2006-03-20 12:37  YCOE  阅读(664)  评论(0编辑  收藏  举报
一个简易的标准JavaBean部分代码生成器

这个功能在Eclipse和Jbuilder中都有,不过用得不爽,自己写一个。玩玩而已,玩玩而已

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Java生成器</title>
</head>
<script language="javascript">
    
var protect = new Array("Abstract","default","goto","native","super","true","boolean","do","if","package","switch","try","break","double","implements","private","synchronized","void","byte","false","import","protected","this","while","byvalue","final","instanceof","public","threadsafe","case","finally","int","return","throw","class","const","float","interface","short","transient");
    
function addCode(){
        
var ve = document.form1.param.value;
        
if(ve.match(/ /)){
            alert(
"不能有空格!");
            document.form1.param.value 
= "";
            document.form1.param.focus();
            
return false;
        }

        
for(var i = 0; i<protect.length;i++){
            
if(ve == protect[i]){
                alert(
"不能使用Java中的保留字或关键字!");
                document.form1.param.value 
= "";
                document.form1.param.focus();
                
return false;
            }

            
//if(i==5);
        }

        
var ns = document.getElementById("select1");
        
var s = document.form1.select1;
        
if(document.form1.type0.value == ""|| ve=="")return false;
        
var h = document.createElement("option");
        h.value 
= document.form1.type0.value;
        ns.appendChild(h);
        h.innerHTML 
= ve;
        creatCode();
    }

    
    
function creatCode(){
        
var ve = document.form1.param.value;
        
var ns = document.getElementById("select1");
        
var s = document.form1.select1;
        
var h = document.createElement("option");
        
        c 
= "";
        
for(ii = 0;ii<s.length;ii++){
                c
+= "private "+s[ii].value+" "+s[ii].innerHTML+";<br>";
                
//alert( s[ii].value);
            }

            c
+="<br>";
        
for(ii = 0;ii<s.length;ii++){
            m 
= s[ii].innerHTML;
            t 
= s[ii].value;
            
//alert();
                c+= "public void set" +m.substring(0,1).toUpperCase()+ m.substring(1,m.length).toLowerCase()+"("+t+" "+m+"){<br>&nbsp;&nbsp;&nbsp;&nbsp;this."+m+" = " + m + ";<br>}<br>public "+ t + " get"+m.substring(0,1).toUpperCase()+ m.substring(1,m.length).toLowerCase()+"(){<br>&nbsp;&nbsp;&nbsp;&nbsp;return "+m+";<br>}<br><br>";
                
//alert( s[ii].value);
            }

        
        document.getElementById(
"code").innerHTML = c;
        document.form1.param.value 
= "";
        document.form1.param.focus();
    }

    
    
function del(){
        
var s = document.form1.select1;
        
for(ii = s.length-1;ii>=0;ii--){
            
if(s[ii].selected){
                s.removeChild(s[ii]);
            }

        }

        creatCode();
    }

    
    
function copy(){
        
//document.getElementById("code").select();
        //document.execCommand('copy'
    }

</script>
<body>
标准JavaBean部分代码生成器:
<form id="form1" name="form1" method="post" action="" onsubmit="addCode(); return false;">
    
<div>
        
<input type="text"  name="param"/>
        
<select name="type0" onchange="addCode()">
            
<option></option>
            
<option value="String">String</option>
            
<option value="int">int</option>
            
<option value="Date">Date</option>
            
<option value="double">double</option>
            
<option value="float">float</option>
            
<option value="long">long</option>
        
</select>
        
<input name="提交" type="button" onClick="addCode(); return false;" value="添加" />
        
<input type="button" value="删除" onclick="del();"/>
            
<select name="select1" size="10" multiple="multiple" style="width:100px;">
              
</select>
    
</div></form>
    
<div id="code"></div>
</body>
</html>


这里只提供了很简单的保留字验证和几个较常用的数据类型而已,玩玩而已的嘛,不要太认真。。。
好啦,使用很简单,把它保存成.htm格式就行了。都说是简易的,当然没什么其它功能了,生成后还得自己去修改一下代码才行。
也没什么好说的了。