【问题标题】:Javascript to Java Applet communicationJavascript 到 Java 小程序的通信
【发布时间】:2011-11-08 20:50:00
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

标签: java javascript applet


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    改变..

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

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

    【讨论】:

    • 嗨,andrew 现在每次我尝试在我的小程序对象中调用 setmethod 时它都无法正常工作,它说对象不支持此方法..是他们调试此方法的任何方式。
    • “它不工作” 上面显示的示例在 FF 中适用于我。 “他们有什么方法可以调试这个(?)” 如果使用 FF,请打开错误控制台(Ctrl+Shift+J)。
    • 是缓存问题。它也对我有用 :) 非常感谢
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-24
        • 1970-01-01
        • 2011-04-14
        • 2010-11-19
        • 2013-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多