JavaScript如何与Applet通讯
Applet1.java
import java.applet.Applet;
import java.awt.*;
public class Applet1 extends Applet {
/**
* @param args the command line arguments
*/
public String UserName= "Jdeveloper";
public void paint(Graphics g) {
g.drawString("Hello:"+UserName,20,20);
}
public void setUserName(String UserName){
this.UserName = UserName;
}
public String getUserName(){
return UserName;
}
}
把下列代码加入<head>…</head>中:
<script language="JavaScript">
<!--
function changeUserName(){
aaa = new String(document.form1.UserName.value);
document.myApplet.setUserName(aaa);
document.myApplet.repaint();
return false;
}
//-->
</script>
把下列html代码加入<body>…</body>中:
<table>
<tr>
<td align="center" >
<applet ID=myApplet Name=myApplet code=Applet1 width=120 height=30>
</applet>
</td>
<td align="center" valign="bottom" nowrap>
<form name=form1 onSubmit="return changeUserName();">
Your Name: <input name=UserName size=10>
<input type=Button name=Btn value="Change Name" language="JavaScript" onclick="return changeUserName();">
</form>
</td>
</tr>
</table>