天天看點

在applet調用js時出現錯誤(已解決)

import java.security.AccessController;

import java.security.PrivilegedAction;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.swing.JApplet;

import netscape.javascript.JSObject;

public class AppletTest extends JApplet{

private static final long serialVersionUID = 2171316410722327511L;

public AppletTest() {

System.out.println("AppletTest============1");

}

public void init() {

System.out.println("AppletTest============2");

new Thread(){

public void run(){

try{

while(true){

Thread.sleep(1000);

System.out.println("init=======================" + getTime());

}

}

catch(Exception e){

e.printStackTrace();

}

}

}.start();

String parame = "{\"success\":true,\"info\":\"加載完成\"}";

System.out.println("加載完成=======================" + getTime());

[color=red]runJS("initFinish", parame); //調用用戶端js方法的[/color]

}

public void myMethod(final String json, final String fun){

AccessController.doPrivileged(new PrivilegedAction<Object>() {

public Object run() {

new Thread(){ // 另起線程,避免用戶端等待applet傳回資料卡死

public void run(){

try{

System.out.println("1Thread=======================" + getTime());

String parame = "{\"success\":true,\"info\":\"執行完畢\"}";

runJS(fun, parame); //調用用戶端js方法的

}

catch(Exception e){

e.printStackTrace();

}

}

}.start();

System.out.println("2Thread=======================" + getTime());

return null;

}

});

}

[color=blue]public void runJS(final String jsFun, final String parame) {

new Thread(new Runnable() {

public void run() {

try {

String fun = "";

if(jsFun != null && !jsFun.equals("")){

fun = jsFun + "(" + parame + ")";

JSObject.getWindow(AppletTest.this).eval("javascript:" + fun + ";");

}

} catch (Throwable e) {

System.out.println("調用js出錯=========" + e.getMessage());

}

}

}).start();

}[/color]

public void destroy() {

super.destroy();

System.out.println("================== destroy");

System.exit(0);

}

public static String getTime() {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return sdf.format(new Date());

}

}

[color=violet]html檔案如下:[/color]

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>

<script>

var getTime = function(formatStr, date){

formatStr = arguments[0] || "yyyy-MM-dd HH:mm:ss";

date = arguments[1] || new Date();

var str = formatStr;

var Week = ['日','一','二','三','四','五','六'];

str=str.replace(/yyyy|YYYY/,date.getFullYear());

str=str.replace(/yy|YY/,(date.getYear() % 100)>9?(date.getYear() % 100).toString():'0' + (date.getYear() % 100));

str=str.replace(/MM/,date.getMonth()>9?(date.getMonth() + 1):'0' + (date.getMonth() + 1));

str=str.replace(/M/g,date.getMonth());

str=str.replace(/w|W/g,Week[date.getDay()]);

str=str.replace(/dd|DD/,date.getDate()>9?date.getDate().toString():'0' + date.getDate());

str=str.replace(/d|D/g,date.getDate());

str=str.replace(/hh|HH/,date.getHours()>9?date.getHours().toString():'0' + date.getHours());

str=str.replace(/h|H/g,date.getHours());

str=str.replace(/mm/,date.getMinutes()>9?date.getMinutes().toString():'0' + date.getMinutes());

str=str.replace(/m/g,date.getMinutes());

str=str.replace(/ss|SS/,date.getSeconds()>9?date.getSeconds().toString():'0' + date.getSeconds());

str=str.replace(/s|S/g,date.getSeconds());

return str;

}

var appletObj = function(){

this.myMethod = function(json, fun){

try{

this.getInstance().myMethod(json, fun);

console.log(getTime() + "======myMethod:");

}

catch(e){

console.error(getTime() + "====applet程式挂了,請重新整理頁面:" + e);

}

}

var tool;

this.getInstance = function(){

console.log(getTime() + "======typeof tool:" + (typeof tool));

console.log(getTime() + "======tool:" + tool);

if(typeof tool == 'undefined'){

console.log(getTime() + "======getInstance:");

tool = $("#tool")[0];

}

return tool;

}

return this;

}();

$(function(){

$("#btn").click(function(){

var parame = {data1:'test1'};

appletObj.myMethod(JSON.stringify(parame), "resultData");

});

});

function resultData(jsonObj){

console.log(getTime() + "========jsonObj:" + JSON.stringify(jsonObj));

console.log(getTime() + "======btn1Click:" + jsonObj.success);

}

function initFinish(jsonObj){

console.log(getTime() + "========applet init end:" + jsonObj.info);

}

</script>

</head>

<body>

<applet id="tool" code="AppletTest" codebase="." archive="AppletTest.jar" width="0" height="0" >

</applet>

<input type="button" id="btn" value="調用applet" />

</body>

</html>

[i][b][size=medium][color=red]解決辦法,在jdk或者jre下面找到jar: deploy.jar,引入到項目下面就可以了。[/color][/size][/b][/i]如果沒有這個jar,建議你重新下載下傳一個jre或者jdk.