关于Applet之间通信的程序的一些小问题

来源:百度知道 编辑:UC知道 时间:2024/06/22 07:23:48
//Applet8.java
import java.awt.*;
import java.applet.*;
public class Applet8 extends Applet{
Label label;
public void init(){
label=new Label("Hello Applet");
add(label);
setBackground(Color.BLUE);
setForeground(Color.WHITE);
}
public void moveLabel(int x,int y){
label.setLocation(x, y);
}
}

============================================
//Applet8_Controller.java
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Applet8_Controller extends Applet implements MouseMotionListener{
Applet8 applet8;
public void init(){
addMouseMotionListener(this);
setBackground(Color.GRAY);
}
public void mouseMoved(MouseEvent e){
if(applet8==null)
applet8=(Applet8)getAppletContext().getApplet("applet8");
applet8.moveLabel(e.getX(), e.getY());
}
public void mouseDragged(MouseEvent e){}
}

getAppletContext()方法是applet类的一个方法
作用:返回一个AppletContext类对象,确定此 applet 的上下文,上下文允许 applet 查询和影响它所运行的环境。
(applet 的环境代表包含该 applet 的文档。)

getApplet(Sting name)方法是AppletContext类的一个方法
作用:使用给定的名称找到并返回此 applet 上下文所代表的文档中的 applet;如果未找到,则返回null

所以语句getAppletContext().getApplet("applet8")是没有问题的

至于语句前面加了(Applet8)我猜想应该是起到实例化的作用(汗!仅供参考)

sun都不对applet开发了