如何从 JavaScript 调用小程序方法 [英] How to call an applet method from JavaScript

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

问题描述

我已经创建了一个小程序,我要从我的 HTML 页面访问一个 web 项目的小程序方法.

I have created an applet and I am going to access an applet method from my HTML page on a 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>

但是当我点击单选按钮时,我的浏览器挂了,我永远无法访问小程序方法.

But when I click radio button my browser hangs and I cannot access the applet method ever.

我的小程序类位于默认目录中,而 HTML 位于 WebContent 文件夹中.我应该在代码中更改哪些内容?

My applet class is in default directory and HTML is in WebContent folder. What should I change in my code?

推荐答案

问题在于if语句检查:

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

这与 JavaScript 不完全一样,因为您在那里使用的错误表达式会引发 error 并且它永远不会将其放入 if 语句主体中.

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 语句并将代码更改为如下所示:

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>

同时添加完整的类代码:

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 调用小程序方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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