Solution
Download LabeledTextFieldTest source code.
HTML Interface to Applet
<APPLET CODEBASE="/applets/magelang/AWT-Training/classes/" CODE="LabeledTextFieldTest.class" WIDTH=300 HEIGHT=20 ALIGN=CENTER></APPLET>
Java Code
import java.awt.*;
public class LabeledTextFieldTest extends java.applet.Applet {
public void init() {
setLayout(new GridLayout(1,1));
LabeledTextField test = new LabeledTextField("Label");
add(test);
}
}
/**
* Component containing both a Label and a TextField
* Subclass this to provide specific fields or to validate
* text entered.
*/
class LabeledTextField extends FormElement {
protected TextField text;
protected Label label;
public LabeledTextField(String s) {
setLayout(new BorderLayout(5,0));
label = new Label(s);
text = new TextField();
add("West", label);
add("Center", text);
}
public boolean isEmpty() {
return text.getText().equals("");
}
/**
* Return a text representation of this field.
* This method is required by the FormElement interface.
* @return String the selected text
*/
public String getContents() {
return label.getText() + " " + text.getText();
}
}
|