|
|
|
|
|
|
SolutionDownload PhoneTextFieldTest source code.HTML Interface to Applet<APPLET CODEBASE="/applets/magelang/AWT-Training/classes/" CODE="PhoneTextFieldTest.class" WIDTH=300 HEIGHT=20 ALIGN=CENTER></APPLET> Java Codeimport java.awt.*; import java.util.*; public class PhoneTextFieldTest extends java.applet.Applet { public void init() { setLayout(new GridLayout(1,1)); PhoneTextField test = new PhoneTextField(); add(test); } } /** * Component containing both a Label and a TextField. * Used for entering and validating a phone number. */ class PhoneTextField extends LabeledTextField { public PhoneTextField() { super("Phone Number"); } public void verify() { StringBuffer buffer = new StringBuffer(); StringTokenizer st = new StringTokenizer(text.getText(), "()- "); // remove parentheses, spaces and dashes while (st.hasMoreTokens()) { buffer.append(st.nextToken()); } // make sure there are numbers try { Integer.parseInt(buffer.toString()); } catch (NumberFormatException e) { System.err.println("Only digits are allowed: " + "Please enter again."); text.setText(""); } // right length? if (buffer.length() != 7 && buffer.length() != 10) { System.err.println("Not the right number of digits: " + "Please enter again."); text.setText(""); } } } |