简体   繁体   English

如何从JavaScript调用Applet方法

[英]How to call Applet method from javascript

I have created an Applet and I am going to access applet method from my html page on web project. 我已经创建了一个Applet,我将从Web项目上的html页面访问applet方法。

Here my applet looks like: 这里我的小程序看起来像:

public class MessageApplet extends Applet {
private Label m_mess;    
    public void init() 
    {
        setBackground(Color.lightGray);
        setLayout(new BorderLayout());
        m_mess = new Label("MessageApplet is Running... : No Selection Yet", Label.CENTER);
        add(BorderLayout.CENTER, m_mess);
    }    
    public void setMessage(String message)
    {
        m_mess.setText("Selection : " + message);
    } 
} 

And my html page looks like: 我的html页面如下所示:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<SCRIPT LANGUAGE="JavaScript">
function selectedCity() 
{
    if(document.CityChoice.City[0].checked == true)
      {
        document.SimpleMessageApplet.setMessage(document.CityChoice.City[0].value);
    }      
}
</SCRIPT></HEAD>
<BODY >
<b>This is the Applet</b>
<APPLET CODE="MessageApplet.class" NAME="SimpleMessageApplet" WIDTH=350 HEIGHT=100 >
</APPLET >
<FORM NAME="CityChoice">
<input type="radio" name="City" value="Boston" onClick="selectedCity()"> Boston<br>
</form>
</BODY > 
</html>

but when I click radio button my browser get hang and I cannot access applet method ever. 但是,当我单击单选按钮时,我的浏览器挂起,并且无法访问applet方法。 My applet class is in default directory and html is in WebContent folder. 我的applet类位于默认目录中,而html位于WebContent文件夹中。 Please tell me what should I have change in my code? 请告诉我我的代码应该更改什么?

The problem is the IF statement check: 问题是IF语句检查:

document.CityChoice.City[0].checked == true

This is not exactly how it goes with Javascript since the wrong expression you have there throws an Error and it never makes it into the IF statement body. 这与Javascript并不完全一样,因为那里的错误表达式会引发错误,并且永远不会使它进入IF语句主体。

I removed the IF statement and changed the code to something like this: 我删除了IF语句,并将代码更改为如下所示:

function selectedCity() 
{
    document.SimpleMessageApplet.setMessage("Hello");                
}

When I click I see the Hello message fine. 当我单击时,我会看到Hello消息很好。

Change your HTML file content to something like: 将您的HTML文件内容更改为类似以下内容:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
        <SCRIPT LANGUAGE="JavaScript">
            function selectedCity() 
            {
                var elem = document.getElementById('cityRb');

                if(elem.checked)
                {
                    document.SimpleMessageApplet.setMessage(elem.value);
                }      
            }
        </SCRIPT></HEAD>
    <BODY >
        <b>This is the Applet</b>
    <APPLET CODE="MessageApplet.class" NAME="SimpleMessageApplet" WIDTH=350 HEIGHT=100 >
    </APPLET >
    <FORM NAME="CityChoice">
        <input type="radio" id="cityRb" name="City" value="Boston" onClick="selectedCity()"> Boston<br>
    </form>
</BODY > 
</html>

Also adding the full class code: 同时添加完整的类代码:

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Label;

/**
 *
 * @author hmmmmm
 */
public class MessageApplet extends Applet {

    private Label m_mess;

    public void init() {
        setBackground(Color.lightGray);
        setLayout(new BorderLayout());
        m_mess = new Label("MessageApplet is Running... : No Selection Yet", Label.CENTER);
        add(BorderLayout.CENTER, m_mess);
        m_mess.setBackground(Color.red);
    }

    public void setMessage(String message) {
        m_mess.setText("Selection : " + message);
    }
}

The problem you get is on different browsers is that they have different implementations of the outdated LiveConnect (javascript<->java) technology. 您在不同的浏览器上遇到的问题是,它们具有过时的LiveConnect(javascript <-> java)技术的不同实现。

As a rule of thumb, Firefox will be more cooperative when trying to do such things. 根据经验,Firefox在尝试执行此类操作时会更加合作。

What your problem is is that you are trying to include your applet to the page in a very ancient way. 您的问题是您试图以一种非常古老的方式将小程序包含到页面中。 Although it may and will work on some browsers, it is not the recommended way to include an applet to a page. 尽管它可能并且将在某些浏览器上运行,但是建议不要在页面中包含小程序。

Java Web start is the tech stack and JNLP is the protocol you can use to distribute java content in a standardized way as you can read in this article: Java Web start是技术堆栈,JNLP是可用于以标准化方式分发Java内容的协议,您可以在本文中阅读:

http://en.wikipedia.org/wiki/Java_Web_Start http://en.wikipedia.org/wiki/Java_Web_Start

A deployment jnlp descriptor is the proper way you can use to embed your applet to a page. 部署jnlp描述符是将applet嵌入页面的正确方法。 Also, it is a good idea to use Sun's deployJava.js script which will save you a lot of trouble when deploying your applet to a container on the page. 同样,使用Sun的deployJava.js脚本也是一个好主意,它将在将Applet部署到页面上的容器时为您省去很多麻烦。 (it's a bit restricted though so feel free to add stuff to it) (虽然有点受限制,所以可以随意添加内容)

http://docs.oracle.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html http://docs.oracle.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html

All in all, a jnlp/Java web start powered applet is the way to go.Below is an example of a deployment descriptor. 总而言之,采用jnlp / Java Web启动技术的applet是必经之路。下面是部署描述符的示例。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <jnlp codebase="file:/C:/JavaApplication6/dist/" href="launch.jnlp" spec="1.0+">
        <information>
            <title>JavaApplication6</title>
            <description>blalbla</description>
            <description kind="short">JavaApplication6</description>

        </information>
    <update check="background"/>
    <security>
    <all-permissions/>
    </security>
        <resources>
    <j2se java-vm-args="-Djava.security.policy=applet.policy" version="1.5+"/>
    <jar href="JavaApplication6.jar" main="true"/>


        <jar href="lib/jna.jar"/>
    <jar href="lib/platform.jar"/>
    </resources>
        <applet-desc height="300" main-class="winToJnaApi.NewApplet" name="JavaApplication6" width="300">

        </applet-desc>
    </jnlp>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM