收藏本页 | 网站地图 | 投稿指南
 
 
当前位置:首页 >> 学院首页 >> 程序开发 >> JSP >>

JAVA/JSP学习系列之十三(Applet调用JavaScript函数)(译)

放大字体  缩小字体  At: 2005-10-04 03:00  By: master8 转载 来源: 互联网
下面的例子将说明在applet中去调用javascript函数 。 一、applet源代码(tmin_JS.java)

// Importations
import java.awt.Graphics ;
import java.awt.Event ;

// LiveConnect... for JavaScript
import netscape.javascript.JSObject ;

public class tmin_JS extends java.applet.Applet {
// Variables

// Initialisation de l"applet
public void init() { // Methode init()
}

// Dessiner l"applet
public void paint(Graphics g) { // Methode paint()
g.drawString("Click here...", 5, 10) ;
}

// Mouse down
public boolean mouseDown(Event e, int x, int y) {
try { // create JSObject
JSObject.getWindow (this).eval ("javascript:ale
rt("tmin_JS click " +
" x=" + x + " y=" + y + "")") ;
}
catch (Exception ex) { // Error on create JSObject
showStatus( "Error call javascript err=" + ex );
}
return true ;
}

}

二、注意的地方

(1)在调用javascipt函数的时候,要加这行:

JSObject.getWindow (this).eval ("javascript:... ") ;

(2)要将JSObject引用进来:

import netscape.javascript.JSObject ;

(3)定义applet的时候要加“MAYSCRIPT”标记

<APPLET codeBase="./" code=tmin_JS width=80 height=25 MAYSCRIPT>

 






         









 
Google
论坛精华  
 
 
  ©2005-2008 站长吧 Master8.NET All Rights Reserved 陕ICP备05010609号