Solution
Download ContactInfoFormTest source code.
HTML Interface to Applet
<APPLET CODEBASE="/applets/magelang/AWT-Training/classes/" CODE="ContactInfoFormTest.class" WIDTH=300 HEIGHT=150 ALIGN=CENTER></APPLET>
Java Code
import java.applet.*;
import java.awt.*;
public class ContactInfoFormTest extends Applet {
public void init() {
add(new ContactInfoForm());
}
}
class ContactInfoForm extends FormElement {
int n=4; // how many fields in the form?
FormElement[] fields = new FormElement[n];
public ContactInfoForm() {
setLayout(new GridLayout(0,1,10,10));
int i=0;
fields[i++] = new LabeledTextField("Name");
fields[i++] = new PhoneTextField();
fields[i++] = new EMailTextField();
fields[i++] = new LabeledChoice("Payment Method");
for (i=0; i<n; i++) {
add(fields[i]);
}
}
public Dimension preferredSize() {
return new Dimension(300,n*30);
}
public String getContents() {
String contents="";
for (int i=0; i<n; i++) {
contents += fields[i].getContents();
if ( i<n-1 ) contents += "\n";
}
return contents;
}
// if any field is empty, say that the form is empty
public boolean isEmpty() {
for (int i=0; i<n; i++) {
if ( fields[i].isEmpty() ) return true;
}
return false;
}
// verify each field
public void verify() {
for (int i=0; i<n; i++) {
fields[i].verify();
}
}
}
|