当前位置: 技术问答>java相关
请问在网页中两个Applet如何互相调用
来源: 互联网 发布时间:2015-01-08
本文导语: 我在网页中加入了两个Applet,现在其中一个Applet想调用另一个Applet中的方法,请问如何实现这一个调用。本人菜鸟,最好能有例子说明,多谢。 | First, you need to make sure you name your applet ...
我在网页中加入了两个Applet,现在其中一个Applet想调用另一个Applet中的方法,请问如何实现这一个调用。本人菜鸟,最好能有例子说明,多谢。
|
First, you need to make sure you name your applet in the APPLET tag using the NAME attribute. The NAME attribute allows other applets on this page to find and communicate with it. For example:
Then, in your other applet, you use the getApplet() method of the AppletContext object:
Applet otherApplet = getAppletContext().getApplet("UserInfo");
What you need to remember is to cast the object returned from getApplet() to the class of object it really is, in order to access any of the applets methods. Just to be safe you could verify that the applet is the class type you expect.
if (otherApplet instanceof UserInfoApplet) {
UserInfoApplet userInfo = (UserInfoApplet) otherApplet;
userInfo.setUserName("Bob");
}
Then, in your other applet, you use the getApplet() method of the AppletContext object:
Applet otherApplet = getAppletContext().getApplet("UserInfo");
What you need to remember is to cast the object returned from getApplet() to the class of object it really is, in order to access any of the applets methods. Just to be safe you could verify that the applet is the class type you expect.
if (otherApplet instanceof UserInfoApplet) {
UserInfoApplet userInfo = (UserInfoApplet) otherApplet;
userInfo.setUserName("Bob");
}