實(shí)現(xiàn)方法一
applet實(shí)現(xiàn)頁(yè)面是rmb.jsp,此頁(yè)面是通過(guò)applet來(lái)實(shí)現(xiàn)人民幣小寫轉(zhuǎn)換成大寫的。此頁(yè)面有三個(gè)變量需要在載入classes的時(shí)候進(jìn)行初始化:
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的背景色跟自己頁(yè)面的風(fēng)格一樣;變量RGB_Fore是初始化字體的顏色; WIGTH要和applet的寬度一樣,這個(gè)變量是保證顯示的轉(zhuǎn)換信息在中間顯示。
此種方法不能傳遞值給頁(yè)面trans.jsp,只能顯示并且不能copy。
頁(yè)面代碼如下:
html>
head>人民幣小寫轉(zhuǎn)換成大寫/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>
實(shí)現(xiàn)方法二
applet實(shí)現(xiàn)頁(yè)面是rmb.jsp,此頁(yè)面是通過(guò)applet來(lái)實(shí)現(xiàn)人民幣小寫轉(zhuǎn)換成大寫的。此頁(yè)面不需要變量進(jìn)行初始化,在頁(yè)面上不顯示applet畫的圖像;而可以把轉(zhuǎn)換的值傳遞給頁(yè)面變量:
applet type="applet" id="myApplet" codebase = "." name="TestApplet"
align="middle" code="lqh.rmb.Applicate.Rmb.class"
width="0" height="0">
/applet>
頁(yè)面trans.jsp代碼如下:
html>
head>人民幣小寫轉(zhuǎn)換成大寫/head>
%@ page contenttype="text/html;charset=gb2312" %>
%@ include file="../rmb.jsp" %>
SCRIPT language=JavaScript>
function trans(){
var aaa=document.rmb. rmb_test.value;
//得到轉(zhuǎn)換的值
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>
轉(zhuǎn)換結(jié)果:input type=”text” name=”rmb_result” >br>
/form>
/body>
/html>
附錄
1.applet代碼:
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="輸入錯(cuò)誤:"+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="輸入錯(cuò)誤:"+rmb_temp;
}
return re_rmb;
}
public boolean checkNum(String str){
String numChar="0123456789.";
boolean breakBoolean = false;
for(int i=0;istr.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
您可能感興趣的文章:- js文件中調(diào)用js的實(shí)現(xiàn)方法小結(jié)
- htm調(diào)用JS代碼
- 在父頁(yè)面調(diào)用子頁(yè)面的JS方法
- kettle中使用js調(diào)用java類的方法
- jsp中調(diào)用java代碼小結(jié)
- 使用jsp調(diào)用javabean實(shí)現(xiàn)超簡(jiǎn)單網(wǎng)頁(yè)計(jì)算器示例
- js調(diào)用后臺(tái)servlet方法實(shí)例
- js中iframe調(diào)用父頁(yè)面的方法
- js與applet相互調(diào)用的方法