在一个HTML页面中,两个或多个Applet可以通过什么方法相互通信?( )
多个小程序不能在一个HTML页面内相互通信
getCodeBase()
getDefaultContext()
getAppletContext()
希望实现一个Applet与另外一个Applet进行通讯时,必须用getAppletContext()方法先取得另外一个Applet的句柄,再用getAplplet(name)方法取得Applet,具体代码实例如下:
AppletContext ac = getAppletContext();
Applet applet = ac.getContext(“other”);
TextArea text = (TextArea)applet.getComponent(2);
Text.append(“good luck”+”\n”);
其中other是另外一个Applet的名字:
<html>
<applet code = “applet.java” width=”300” height=”200” name=”other”></applet>
</html>
而getComponent(2)中的参数“2”是指添加到Applet中的组件的次序(从1开始);
这样我们就能操纵另外一个Applet中的组件了。
AppletContext ac = getAppletContext();
Applet applet = ac.getContext(“other”);
TextArea text = (TextArea)applet.getComponent(2);
Text.append(“good luck”+”\n”);
其中other是另外一个Applet的名字:
<html>
<applet code = “applet.java” width=”300” height=”200” name=”other”></applet>
</html>
而getComponent(2)中的参数“2”是指添加到Applet中的组件的次序(从1开始);
这样我们就能操纵另外一个Applet中的组件了。