关于java里applet的问题
发布网友
发布时间:2022-04-30 15:56
我来回答
共1个回答
热心网友
时间:2022-04-15 09:31
applet可以调用另一个和。
一个简单例子
//Applet-同一页面的两applet通信.txt
1、第一个Applet
//: Applet1.java
// Very simple applet
package xjtu.citi;
import java.awt.*;
import java.applet.*;
public class Applet1 extends Applet {
public void paint(Graphics g) {
g.drawString("First applet,What can I do?", 20, 30);
//获得同一页面的applet
Applet2 theOtherApplet =(Applet2)getAppletContext().getApplet("app2");
//使用另一个applet的方法
g.drawString(theOtherApplet.whatidoallforyou(),20,60);
//获得同一页面的所有applet
Enumeration allAppletsOnSamePage = getAppletContext().getApplets();
while(allAppletsOnSamePage.hasMoreElements()) {
Applet appl = (Applet) allAppletsOnSamePage.nextElement();
appl.paint(g); //调用任一个公用方法
}
}
} ///:~
2、第二个Applet
//: Applet2.java
// Easy graphics
package xjtu.citi;
import java.awt.*;
import java.applet.*;
public class Applet2 extends Applet {
public void paint(Graphics g) {
g.drawString("Second applet", 10, 15);
g.draw3DRect(0, 0, 100, 20, false);
}
public String whatidoallforyou(){
return "what I do all for you,zly";
}
} ///:~
3、Html文件:twoapplets.html
<html>
<applet codebase = "." code="xjtu.citi.Applet2" width="400" height="200" name="app2">
</applet>
<br>
<applet codebase = "." code="xjtu.citi.Applet1" width="400" height="200" name="app1">
</applet>
<br>
</html>