// Written by: Darren Gates & Ray Klefstad // Date: January 2000 // School: U.C. Irvine import java.awt.*; import java.awt.event.*; import java.applet.*; public class CharacterStringsExample extends Applet { private TextField testField; public void init() { setLayout(new GridLayout(3, 1)); // a panel containing only the test field Panel testPanel = new Panel(); testField = new TextField("Hello World!", 20); testPanel.add(testField); CharAt CharAtTest = new CharAt("CharAt"); Concat ConcatTest = new Concat("Concat"); add(testPanel); add(CharAtTest); add(ConcatTest); } class CharAt extends Panel implements ActionListener { private Button myButton; private TextField myTextField; private TextField result; public CharAt(String name) { myButton = new Button(name); myTextField = new TextField(5); result = new TextField(10); add(myButton); add(myTextField); add(result); myButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { try { int indexNum = Integer.parseInt(myTextField.getText()); result.setText("" + testField.getText().charAt(indexNum)); } catch(NumberFormatException error) { result.setText("Bad Format"); } catch(IndexOutOfBoundsException error) { result.setText("Out of Range"); } } } class Concat extends Panel implements ActionListener { private Button myButton; private TextField myTextField; private TextField result; public Concat(String name) { myButton = new Button(name); myTextField = new TextField(10); result = new TextField(20); add(myButton); add(myTextField); add(result); myButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { result.setText(testField.getText() + myTextField.getText()); } } }