欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jsp页面调用applet实现人民币的大小写转换

程序员文章站 2023-12-16 15:26:40
实现方法一 ...

实现方法一

 

applet实现页面是rmb.jsp,此页面是通过applet来实现人民币小写转换成大写的。此页面有三个变量需要在载入classes的时候进行初始化:

<applet  type="applet" id="myapplet" codebase = "." name="testapplet"

 align="middle" code="lqh.rmb.applicate.rmb.class"

width="560" height="25">

<param name="rgb_back" value="241|247|251">

<param name="rgb_fore" value="255|0|0"/>

<param name="wigth" value="560"/>

</applet>

变量rgb_back是初始化背景色的,修改次变量可以使applet的背景色跟自己页面的风格一样;变量rgb_fore是初始化字体的颜色; wigth要和applet的宽度一样,这个变量是保证显示的转换信息在中间显示。

此种方法不能传递值给页面trans.jsp,只能显示并且不能copy

 

页面代码如下:

<html>

<head>人民币小写转换成大写</head>

<%@ page contenttype="text/html;charset=gb2312" %>

<script language=javascript>

function trans(){

   //取变量的值

   var aaa=document.rmb. rmb_test.value;

   //applet赋变量的值

   document.myapplet.setrmb(aaa);

   document.myapplet.repaint();

   return true;

}

</script>

<body>

<form name="rmb">

 输入金额:<input type=”text” name=”rmb_test”  onkeyup=”trans()”><br><hr>

 <%@ include file="../rmb.jsp" %>

</form>

</body>

</html>

      

实现方法二

 

applet实现页面是rmb.jsp,此页面是通过applet来实现人民币小写转换成大写的。此页面不需要变量进行初始化,在页面上不显示applet画的图像;而可以把转换的值传递给页面变量:

<applet  type="applet" id="myapplet" codebase = "." name="testapplet"

     align="middle" code="lqh.rmb.applicate.rmb.class"

width="0" height="0">

</applet>

 

页面trans.jsp代码如下:

<html>

<head>人民币小写转换成大写</head>

<%@ page contenttype="text/html;charset=gb2312" %>

<%@ include file="../rmb.jsp" %>

<script language=javascript>

function trans(){  

   var aaa=document.rmb. rmb_test.value;

   //得到转换的值

   var rmb = document.myapplet.getrmb(aaa);

   document.rmb. rmb_result.value = rmb;

   return true;

}

</script>

<body>

<form name="rmb">

  输入金额:<input type=”text” name=”rmb_test”  onkeyup=”trans()”><br>

转换结果:<input type=”text” name=”rmb_result” ><br>

</form>

</body>

</html>

附录

1applet代码:

 

package lqh.rmb.applicate;

 

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

import lqh.rmb.*;

 

public class rmb extends applet {

  private boolean isstandalone = false;

  private rmbconversion rmb=new rmbconversion();

  private  string var0 = "";

  private  string re_rmb="";

  private  string wigth ="0";

  private string back_color;

  private string fore_color;

 

  public string getparameter(string key, string def) {

   return isstandalone ? system.getproperty(key, def) :

     (getparameter(key) != null ? getparameter(key) : def);

  }

  public void paint(graphics g) {

    if(wigth.equals("0")){

      wigth = "560";

    }

    int i= integer.parseint(wigth);

 

    if(checknum(var0)){

      if(var0.endswith(".")){

       var0 = var0.substring(0,var0.length()-1);

      }

      re_rmb= rmb.rmb(var0);

    }else{

      re_rmb="输入错误"+var0;

    }

    int middel =(i - re_rmb.length()*12)/2;

    this.setbackground(new color(getcolor(back_color)[0], getcolor(back_color)[1], getcolor(back_color)[2]));

    this.setforeground(new color(getcolor(fore_color)[0], getcolor(fore_color)[1], getcolor(fore_color)[2]));

    g.drawstring(re_rmb,middel,20);

  }

  public void init() {

    try {

      back_color = this.getparameter("rgb_back", "241|247|251");

       }catch(exception e) {e.printstacktrace();}

   try {

     fore_color = this.getparameter("rgb_fore", "255|0|0");

     }catch(exception e) {e.printstacktrace();}

   try {

     wigth = this.getparameter("wigth", "0");

     }catch(exception e) {e.printstacktrace();}

   try {

     jbinit();

   }catch(exception e) { e.printstacktrace(); }

 }

  private void jbinit() throws exception {

   this.setbackground(new color(getcolor(back_color)[0], getcolor(back_color)[1], getcolor(back_color)[2]));

  }

  public void setrmb(string rmb_temp){

    var0 = rmb_temp;

  }

  public string getrmb(string rmb_temp){

    if(checknum(rmb_temp)){

      if(rmb_temp.endswith(".")){

       rmb_temp = rmb_temp.substring(0,rmb_temp.length()-1);

      }

      re_rmb= rmb.rmb(rmb_temp);

    }else{

      re_rmb="输入错误"+rmb_temp;

    }

    return re_rmb;

  }

  public boolean checknum(string str){

    string numchar="0123456789.";

    boolean breakboolean = false;

    for(int i=0;i<str.length();i++){

      if(numchar.indexof(str.substring(i,i+1))<0){

        breakboolean =true;

      }

      if(breakboolean) break;

    }

    if(breakboolean){

      return false;

    }else{

      if(str.startswith(".")){

        return false;

      }else{

        return true;

      }

    }

  }

  public string[][] getparameterinfo() {

    string[][] pinfo =

      {

      {"rgb_back", "string", ""},

      {"rgb_fore", "string", ""},

      {"wigth",    "string", ""},

      };

    return pinfo;

  }

  private int[] getcolor(string rgb){

   int[] rgb_return = new int[3];

   int index =0,nextindex=0,len=0;

   while((index =rgb.indexof("|",nextindex))>-1){

    rgb_return[len]=getint(rgb.substring(nextindex,index));

    nextindex=index+1;

    len++;

   }

   rgb_return[len]=getint(rgb.substring(nextindex));

   return rgb_return;

  }

  private int getint(string str){

    try{

      return integer.parseint(str);

    }catch(exception e){

      return 0;

    }

  }

}

2.java包,欢迎索取lqhboy@163.com

上一篇:

下一篇: