LabeledTextField Help
Help is available for each task,
or you can go straight to the LabeledTextField
source code to see a
solution.
Task 1
Create an Applet to hold one instance of LabeledTextField so you can test your component. Set the layout so that any component will be stretched to the applet size.
You need to create a class separate from your LabeledTextField class.
You can call it LabeledTextFieldTest. In this class, create an
instance of LabeledTextField and add it to the container. Here you
should use a GridLayout to
manage the container so that the LabeledTextField will be scaled to
fit the window, rather than appearing at its preferredSize.
import java.awt.*;
public class LabeledTextFieldTest extends java.applet.Applet {
public void init() {
setLayout(new GridLayout(1,1));
LabeledTextField test = new LabeledTextField();
add(test);
}
}
Task 2
Create the class LabeledTextField as a subclass of FormElement.
public class LabeledTextField extends FormElement {
...
}
Task 3
In the class LabeledTextField, create a left-justified Label and a TextField to the right. Make the TextField
fill the entire remaining width of the Panel.
You have to create a constructor to initialize the components needed by
the compound component, and to add them to the current Container.
In this case, the Container is a Panel because FormElement subclasses Panel:
class LabeledTextField extends FormElement {
protected TextField text;
protected Label label;
public LabeledTextField(String s) {
setLayout(new BorderLayout(5,0)); // hgap of 5, no vgap
label = new Label(s);
text = new TextField();
add("West", label); // put label on the left
add("Center", text); // put text on the right
}
...
}
text and label are instance variables so that other methods in this class will have access to them.
Task 4
Implement the method isEmpty of FormElement to return true if the TextField is empty.
public boolean isEmpty() {
return text.getText().equals("");
}
Task 5
Implement the method getContents of FormElement. It should return the Label and the contents of the LabeledTextField.
public String getContents() {
return label.getText() + " " + text.getText();
}
|