可以写一个"容器"类使得两个applet可以相互通信
一个拙劣的参考实现
import java.applet.Applet;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Frame;
public class E{
public A1 a1;
public A2 a2;
public Frame f;
public static void main(String[] args){
E main = new E();
main.f = new Frame("main frame");
main.f.setSize(400, 300);
main.f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
A1 a1 = new A1(main);
a1.init();
a1.start();
main.f.show();
}
}
class A1 extends Applet{
private E controller;
public A1(E controller){
super();
this.controller = controller;
controller.a1 = this;
controller.f.add(this);
controller.f.setTitle("Using by A1");
}
public void init(){
super.init();
Button b = new Button("Start A2");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
controller.a1.stop();
controller.a1.destroy();
controller.a2 = new A2(controller);
controller.a2.init();
controller.a2.start();
controller.f.show();
}
});
this.add(b);
}
public void destroy(){
controller.f.remove(this);
super.destroy();
}
}
class A2 extends Applet{
private E controller;
public A2(E controller){
super();
this.controller = controller;
controller.a2 = this;
controller.f.add(this);
controller.f.setTitle("Using by A2");
}
public void init(){
super.init();
Button b = new Button("Start A1");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
controller.a2.stop();
controller.a2.destroy();
controller.a1 = new A1(controller);
controller.a1.init();
controller.a1.start();
controller.f.show();
}
});
this.add(b);
}
public void destroy(){
controller.f.remove(this);
super.destroy();
}
}
凡是有该标志的文章,都是该blog博主Caoer(草儿)原创,凡是索引、收藏
、转载请注明来处和原文作者。非常感谢。