程序代碼:
Applet
import java
import java
import java
public class Applet
{
TextField tf = new TextField(
Button b = new Button(
public void init()
{
setLayout(new FlowLayout()); // 設置布局管理器為FlowLayout
add(tf);
add(b);
}
public boolean action(Event ev
{
if(ev
{
String msg = tf
Applet
if(applet
{
applet
return true;
}
else
{
tf
return false;
}
}
return false;
}
}
=================================================
Applet
import java
import java
public class Applet
{
TextField text = new TextField(
public void init()
{
setLayout(new FlowLayout());
add(text);
}
public void AppendText(String msg) // <<--- 聲明為公開方法以便applet1調用
{
text.setText("我收到了!是\""+msg+"\"");
}
}
說明:Applet類中可以取得網頁的上下文句柄,因此,同網頁內的兩個Applet是可以互相訪問到的,在本例中注意Applet2的HTLM代碼一定要加上name="Applet2",否則無法取得Applet2得句柄.編譯的時候先編譯Applet2,這樣Applet1中才能引用Applet2這個類.JB下例子通不過,需要在IE中執行.
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26878.html