import java.awt.*; import java.applet.Applet; public class AdjustUpDown extends Applet { Button upButton = new Button("Increment"); Button downButton = new Button("Decrement"); TextField delta = new TextField("1", 5); // Width 5 int counter = 0; public void init() { add(downButton); add(upButton); add(delta); repaint(); } public void paint(Graphics g) { g.drawString("Count: "+counter, 70, 50); } public boolean action(Event e, Object o) { // Might be an up or a down button click // or "Return" on the text field // First, get the value in delta field, resetting if junk int deltaValue; deltaValue = getValue(delta); // Check the event's "target" if (e.target==downButton) counter = counter-deltaValue; else if (e.target==upButton) counter = counter+deltaValue; // If neither down nor up button, no special action repaint(); return true; } // Next, an example of an extra function: // To obtain the number currently in a nominated text field // Resets the field to 1 if it contains junk (and result is 1) private int getValue(TextField tf) { String string; int value; string = tf.getText(); try { value = Integer.parseInt(string); } catch (NumberFormatException ne) { // Junk in field - reset it value = 1; tf.setText("1"); } return value; } }