0

I am trying to create a very simple java Applet to try out using Java functions in a web browser. problem is, is that I can't get any sort of functionality from my applet. I've tried dozens of tutorials and answers from within this site, but nothing produces any different result, The browser always says AppName.FunctionName is not a function.

Here is my html...

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4  /strict.dtd">
<html>
<head>
 <meta content="text/html; charset=ISO-8859-1"
 http-equiv="content-type">
<title>Test Applet</title>
</head>
<body onload="test()">
<applet code="TestApplet.class" name="AppTest"
height="350" width="100"></applet>
<script language="Javascript">
function test(){
alert("Attempt 1");
var elem= document.getElementById('AppTest');
alert(elem);
elem.test();
alert("Attempt 2");
document.AppTest.test();
}
</script><br>
</body>
</html>

and here is my Java code...

import javax.swing.JApplet;

public class TestApplet extends JApplet {

    public String sayHi(){
        return "hello";
    }
    public void test(){
        System.out.println("You did it bro");
    }
}

Any ideas why this seems to do nothing? Note: I am testing it using FireFox

EDIT:

So trying to move closer to a working solution, I modified my html as follows

<html>
<head>
  <title>Test Applet</title>
</head>
<body>
<applet code="AppPack.TestApplet.class"
 codebase="TestApplet.jar" name="AppTest"
 scriptable="true" height="350" width="100"></applet>
<script language="Javascript">function test(){
document.AppTest.test();
}
</script><br>
<input name="tryit" value="TryIt" onclick="test()"
 type="button">
</body>
</html>

I've tried to use the codebase declaration, seeing as how including it draws the applet to the canvas, however the box just contains an error saying class not found exception, and without it I get a blank page with just the button. ALL the files are in the same directory, and if I test the applet in eclipse, it generates html to run the applet in the viewer, but these html files also don't load if opened simply using a broswer. Please, can anyone shed some light on what I am doing wrong?

4

1 回答 1

2

测试函数调用太快。

onload调用页面函数时不一定会加载和启动小程序。这实际上只意味着浏览器已经读取了与页面相关的所有 HTML,不一定是图像、(外部定义的)样式或脚本,或者可能由于页面加载而创建的东西(例如小程序)。

将对该test()方法的调用放入您单击的按钮中,在页面加载后等待“片刻”,然后再试一次。


也一定要scriptable='true'在部署时声明它。像这样:

<applet 
    code="TestApplet" 
    name="AppTest"
    scriptable="true"
    height="350" 
    width="100">
</applet>

大多数浏览器都允许在没有它的情况下调用小程序,但最好为其他浏览器添加它。

于 2013-09-17T16:38:42.610 回答