java.applet.AppletContext类提供了小程序之间的通信功能。我们通过HTML文件提供applet的名称。它提供了getApplet()方法, 该方法返回Applet的对象。句法:
public Applet getApplet(String name){}
小程序通信示例
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ContextApplet extends Applet implements ActionListener{
Button b;
public void init(){
b=new Button("Click");
b.setBounds(50, 50, 60, 50);
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
AppletContext ctx=getAppletContext();
Applet a=ctx.getApplet("app2");
a.setBackground(Color.yellow);
}
}
myapplet.html
<html>
<body>
<applet code="ContextApplet.class" width="150" height="150" name="app1">
</applet>
<applet code="First.class" width="150" height="150" name="app2">
</applet>
</body>
</html>
评论前必须登录!
注册