Javascript 到 Java Applet 通信

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7278626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:33:47  来源:igfitidea点击:

Javascript to Java Applet communication

javajavascriptapplet

提问by bhalkian

I am trying to pass a selected value from HTML drop-down to an Applet method, using setter method in the Applet. But every time the Javascript is invoked it shows "object doesn't support this property or method" as an exception.

我正在尝试使用 Applet 中的 setter 方法将 HTML 下拉列表中的选定值传递给 Applet 方法。但是每次调用 Javascript 时,它都会显示“对象不支持此属性或方法”作为异常。

My javascript code :

我的 javascript 代码:

function showSelected(value){
    alert("the value given from"+value);
    var diseasename=value;
    alert(diseasename);
    document.decisiontreeapplet.setDieasename(diseasename);

    alert("i am after value set ");
}

My applet code :

我的小程序代码:

package com.vaannila.utility;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


import prefuse.util.ui.JPrefuseApplet;

public class dynamicTreeApplet extends JPrefuseApplet {

    private static final long serialVersionUID = 1L;
    public static int i = 1;
    public String dieasenameencode;
    //System.out.println("asjdjkhcd"+dieasenameencode);
    public void init() {
        System.out.println("asjdjkhcd"+dieasenameencode);
        System.out.println("the value of i is " + i);
        URL url = null;
        //String ashu=this.getParameter("dieasenmae");
        //System.out.println("the value of the dieases is "+ashu);

        //Here dieasesname is important to make the page refresh happen 

        //String dencode = dieasenameencode.trim();
        try {
            //String dieasename = URLEncoder.encode(dencode, "UTF-8");
            // i want this piece of the code to be called 
            url = new URL("http://localhost:8080/docRuleToolProtocol/appletRefreshAction.do?dieasename="+dieasenameencode);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            InputStream ois = con.getInputStream();
            this.setContentPane(dynamicView.demo(ois, "name"));
            ois.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException f) {
            f.printStackTrace();

        } catch (IOException io) {
            io.printStackTrace();
        }
        ++i;
    }

    public void setDieasename(String message){
        System.out.println("atleast i am here and call is made ");
        this.dieasenameencode=message;
        System.out.println("the final value of the dieasenmae"+dieasenameencode);

    }

}

My appletdeployment code :

我的小程序部署代码:

<applet id="decisiontreeapplet" code="com.vaannila.utility.dynamicTreeApplet.class" archive="./appletjars/dynamictree.jar, ./appletjars/prefuse.jar" width ="1000" height="500" >    
</applet>

回答by Andrew Thompson

Change..

改变..

document.decisiontreeapplet

..to..

..到..

document.getElementById('decisiontreeapplet')

..and it will most likely work.

..它很可能会起作用。

E.G.

例如

HTML

HTML

<html>
<body>
<script type='text/javascript'>
function callApplet() {
    msg = document.getElementById('input').value;
    applet = document.getElementById('output');
    applet.setMessage(msg);
}
</script>
<input id='input' type='text' size=20 onchange='callApplet()'>
<br>
<applet
    id='output'
    code='CallApplet'
    width=120
    height=20>
</applet>
</body>
</html>

Java

爪哇

import javax.swing.*;

public class CallApplet extends JApplet {

    JTextField output;

    public void init() {
        output = new JTextField(20);
        add(output);
        validate();
    }

    public void setMessage(String message) {
        output.setText(message);
    }
}


Please also consider posting a short complete example next time. Note that the number of lines in the two sources shown above, is shorter that your e.g. applet, and it took me longer to prepare the source so I could check my answer.

下次还请考虑发布一个简短的完整示例。请注意,上面显示的两个源中的行数比您的小程序短,而且我准备源的时间更长,因此我可以检查我的答案。

回答by Perception

Try changing the idparameter in your applet tag to nameinstead.

尝试将小程序标记中的id参数改为name

<applet name="decisiontreeapplet" ...>
</applet>

回答by Daniel Pereira

回答by Rostislav Matl

I think the <applet> tag is obsolete and <object>tag shoudl be used instead. I recall there was some boolean param named scriptablein the object tag.

我认为 <applet> 标签已经过时,应该使用<object>标签代替。我记得在对象标签中有一些名为scriptable 的布尔参数。

Why you do not use deployment toolkit? It would save you a lot of trying - see http://rostislav-matl.blogspot.com/2011/10/java-applets-building-with-maven.htmlfor more info.

为什么不使用部署工具包?它会为您节省很多尝试 -有关更多信息,请参见http://rostislav-matl.blogspot.com/2011/10/java-applets-building-with-maven.html