Javascript 到 Java Applet 通信

标签 java javascript applet

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

我的 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 ");
}

我的小程序代码:

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);

    }

}

我的小程序部署代码:

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

最佳答案

改变..

document.decisiontreeapplet

..到..

document.getElementById('decisiontreeapplet')

..它很可能会起作用。

例如

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);
    }
}

下次还请考虑发布一个简短的完整示例。请注意,上面显示的两个来源中的行数比您的例如短。小程序,我花了更长的时间来准备源代码,以便检查我的答案。

关于Javascript 到 Java Applet 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7278626/

相关文章:

java - Java 中的负界

java - 油漆似乎不是在画东西

java - 如何在 OpenNLP 中通过 NER 识别印度名字?

java - 如何打印数组直到有值。而不是完整数组?

javascript - 移动环境中的网站桌面模式

javascript - 响应未显示 ID 屏幕?

javascript - Ajax - 单击按钮更新数据库

java - 创建要在浏览器中启动的 Java 小程序

java - 在 Mac OS x 10.4 上加载小程序的问题

java - 为什么 HttpUrlConnection 需要 getInputStream 来发送请求?