[英]How to call Applet method from javascript
我已經創建了一個Applet,我將從Web項目上的html頁面訪問applet方法。
這里我的小程序看起來像:
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頁面如下所示:
<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>
但是,當我單擊單選按鈕時,我的瀏覽器掛起,並且無法訪問applet方法。 我的applet類位於默認目錄中,而html位於WebContent文件夾中。 請告訴我我的代碼應該更改什么?
問題是IF語句檢查:
document.CityChoice.City[0].checked == true
這與Javascript並不完全一樣,因為那里的錯誤表達式會引發錯誤,並且永遠不會使它進入IF語句主體。
我刪除了IF語句,並將代碼更改為如下所示:
function selectedCity()
{
document.SimpleMessageApplet.setMessage("Hello");
}
當我單擊時,我會看到Hello消息很好。
將您的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>
同時添加完整的類代碼:
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);
}
}
您在不同的瀏覽器上遇到的問題是,它們具有過時的LiveConnect(javascript <-> java)技術的不同實現。
根據經驗,Firefox在嘗試執行此類操作時會更加合作。
您的問題是您試圖以一種非常古老的方式將小程序包含到頁面中。 盡管它可能並且將在某些瀏覽器上運行,但是建議不要在頁面中包含小程序。
Java Web start是技術堆棧,JNLP是可用於以標准化方式分發Java內容的協議,您可以在本文中閱讀:
http://en.wikipedia.org/wiki/Java_Web_Start
部署jnlp描述符是將applet嵌入頁面的正確方法。 同樣,使用Sun的deployJava.js腳本也是一個好主意,它將在將Applet部署到頁面上的容器時為您省去很多麻煩。 (雖然有點受限制,所以可以隨意添加內容)
http://docs.oracle.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html
總而言之,采用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.