Phương thức getApplet tìm kiếm tất cả các applet khác trên trang hiện hành để thấy một trong số các applet đó bằng một tên đặc biệt. Nếu tìm thấy, getApplet sẽ trả về một đối tượng Applet.
Mặc định thì một applet khơng có tên. Để đặt cho applet một cái tên, phải đặt trong đoạn mã của trang HTML mà applet đan xen vào. Bạn có thể chỉ rõ tên của một applet bằng hai cách:
Bằng cách chỉ rõ một thuộc tính NAME trong thẻ <APPLET>. Thí dụ như: <APPLET CODEBASE=example/ CODE=Sender.class WIDTH=450
HEIGHT=200 NAME="buddy"> . . .
</applet>
Bằng cách chỉ rõ tham số NAME với thẻ <PARAM>. Thí dụ như: <APPLET CODEBASE=example/ CODE=Receiver.class WIDTH=450
HEIGHT=35>
<PARAM NAME="name" value="old pal">
. . . </applet>
Dưới đây là hai applet minh hoạ cho việc tìm bằng tên. Applet đầu tiên là Sender tìm applet thứ hai là Rêciver, Sender gửi một thông điệp tới Reciever bằng cách gọi các phương thức của Reciever. Reciever đáp ứng lại "Received message from sender-name!"
Đây là chương trình đầy đủ của Sender:
import java.applet.*; import java.awt.*;
import java.awt.event.*; import java.util.Enumeration;
public class Sender extends Applet
implements ActionListener { private String myName;
private TextField nameField; private TextArea status; private String newline; public void init() {
GridBagLayout gridBag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); setLayout(gridBag);
Label receiverLabel = new Label("Receiver name:", Label.RIGHT); gridBag.setConstraints(receiverLabel, c); add(receiverLabel);
nameField = new TextField(getParameter("RECEIVERNAME"), 10);
c.fill = GridBagConstraints.HORIZONTAL; gridBag.setConstraints(nameField, c); add(nameField);
nameField.addActionListener(this);
Button button = new Button("Send message");
c.gridwidth = GridBagConstraints.REMAINDER; //end row c.anchor = GridBagConstraints.WEST; //stick to the //text field c.fill = GridBagConstraints.NONE; //keep the button //small
gridBag.setConstraints(button, c); add(button);
button.addActionListener(this); status = new TextArea(5, 60); status.setEditable(false);
c.fill = GridBagConstraints.BOTH; //make this big c.weightx = 1.0; c.weighty = 1.0; gridBag.setConstraints(status, c); add(status); myName = getParameter("NAME");
Label senderLabel = new Label("(My name is " + myName + ".)", Label.CENTER); c.weightx = 0.0; c.weighty = 0.0; gridBag.setConstraints(senderLabel, c); add(senderLabel); newline = System.getProperty("line.separator"); }
public void actionPerformed(ActionEvent event) { Applet receiver = null;
String receiverName = nameField.getText(); //Get name to //search for. receiver = getAppletContext().getApplet(receiverName); if (receiver != null) {
//Use the instanceof operator to make sure the applet //we found is a Receiver object.
if (!(receiver instanceof Receiver)) { status.append("Found applet named " + receiverName + ", "
+ "but it's not a Receiver object." + newline);
} else {
status.append("Found applet named " + receiverName + newline + " Sending message to it."
+ newline);
//Cast the receiver to be a Receiver object //(instead of just an Applet object) so that the //compiler will let us call a Receiver method. ((Receiver)receiver).processRequestFrom(myName); }
} else {
status.append("Couldn't find any applet named " + receiverName + "." + newline); }
}
public Insets getInsets() { return new Insets(3,3,3,3); }
public void paint(Graphics g) { g.drawRect(0, 0,
getSize().width - 1, getSize().height - 1); }
public String getAppletInfo() {
return "Sender by Kathy Walrath"; }
}
Dưới đây là chương trình Reciever: import java.applet.*;
import java.awt.*;
public class Receiver extends Applet {
private final String waitingMessage="Waiting for a message..."; private Label label = new Label(waitingMessage, Label.RIGHT); public void init() {
add(label);
add(new Button("Clear"));
add(new Label("(My name is " + getParameter("name") + ".)", Label.LEFT));
validate(); }
public boolean action(Event event, Object o) { label.setText(waitingMessage);
repaint(); return false; }
public void processRequestFrom(String senderName) {
label.setText("Received message from " + senderName + "!"); repaint();
public void paint(Graphics g) {
g.drawRect(0, 0, size().width - 1, size().height - 1); }
public String getAppletInfo() {
return "Receiver (named " + getParameter("name") + ") by Kathy Walrath";
} }