Textfield Example

This applet takes input via a textfield.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class AgeCheck extends Applet implements ActionListener
{

private TextField ageField;
private int age;
public void init()
{

ageField = new TextField(10);
add(ageField);
ageField.addActionListener(this);

}
public void actionPerformed(ActionEvent event)
{

age = Integer.parseInt(ageField.getText());
repaint();

}
public void paint(Graphics g)
{

g.drawString("Age is " + age, 50, 50);
if (age >= 18)

g.drawString("You can vote", 50, 100);

else

g.drawString("You cannot vote", 50, 100);

}

}

vote.JPG

D. Bell, M. Parr, Java for Students, 3rd Ed (1998), Pearson

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License