如何从JavaScript调用Applet的方法 [英] How to call Applet method from javascript

查看:122
本文介绍了如何从JavaScript调用Applet的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个小程序,我会从我的html页面访问小程序法上的Web项目。

I have created an Applet and I am going to access applet method from my html page on web project.

下面我的小程序是这样的:

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

和我的HTML页面的样子:

And my html page looks like:

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

但是当我点击单选按钮,我的浏览器中得到挂起,我永远不能访问小程序的方法。
我的小程序类是在默认目录和HTML是WebContent文件夹。
请告诉我,我应该有变化,我的code?

but when I click radio button my browser get hang and I cannot access applet method ever. My applet class is in default directory and html is in WebContent folder. Please tell me what should I have change in my code?

推荐答案

的问题是IF语句检查:

The problem is the IF statement check:

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

这是不是它究竟是如何去使用JavaScript,因为你有没有抛出一个错误,它永远不会使它进入IF语句体错误的EX pression。

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.

我删除了IF语句,改变了code到这样的事情:

I removed the IF statement and changed the code to something like this:

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

当我点击我看到Hello消息罚款。

When I click I see the Hello message fine.

您的HTML文件的内容更改为类似:

Change your HTML file content to something like:

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

还加入了完整的类code:

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

这篇关于如何从JavaScript调用Applet的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆