
import java.applet.*; |
import java.awt.*; |
import java.awt.event.*; |
import java.util.Enumeration; |
public class GetApplets extends Applet implements ActionListener { |
private TextArea textArea; // 声明一个TextArea |
private String newline; |
public void init() { |
Button b = new Button("Click to call getApplets()"); |
b.addActionListener(this); |
setLayout(new BorderLayout()); |
add("North", b); |
textArea = new TextArea(5, 40); |
textArea.setEditable(false); |
add("Center", textArea); |
newline = System.getProperty("line,separator"); |
// 取得系统当前的换行符 |
} |
public void actionPerformed(ActionEvent event) { |
/* Button b点击后的事件处理函数 */ |
printApplets(); |
} |
public String getAppletInfo() { |
return "GetApplets by Dong.li"; |
} |
public void printApplets() { |
Enumeration e = getAppletContext().getApplets(); |
/* 得到当前网页所有的Applet对象 */ |
textArea.append("Results of getApplets():" + newline); |
while (e.hasMoreElements()) { |
Applet applet = (Applet) e.nextElement(); |
String info = ((Applet) applet).getAppletInfo(); |
/* 逐个取得当前网页Applet对象的信息 */ |
if (info != null) { |
textArea.append("-" + info + newline); |
/* 在textArea中输出网页所有Applet的信息 */ |
} else { |
textArea.append("-" + applet.getClass().getName() + newline); |
} |
} |
textArea.append("__________" + newline + newline); |
} |
} |



