Java Applet Example: Capturing and Displaying Personal Data

Computer science1.61 KB

Java Applet for Managing Personal Information

This Java applet lets users enter a first name, paternal surname, maternal surname, and age, then load those values into the display or clear them individually or all at once.

Source Code

The class MichaelHerrera.Michael extends Applet and implements ActionListener.

Declared Components

  • TextField inputs for name, paternal surname, maternal surname, and age
  • Button controls to load or erase each field, plus "Load All" and "Erase All"
  • Variables nombre, Apellido1, Apellido2, and edad that store the loaded values

Initialization

The init() method creates each TextField and Button, adds them to the applet, and registers the applet as an action listener.

Displaying Data

The paint() method draws the stored values on screen at fixed coordinates using drawString.

Handling Events

The actionPerformed() method checks the event source. When a load button is pressed, the matching text is copied into its variable. When an erase button is pressed, the matching field is cleared. Finally, repaint() refreshes the display.

Important Note on Age Input

The age field is converted with Integer.parseInt(). If the user enters a non-numeric value, a NumberFormatException will be thrown. Input validation or exception handling is recommended.

Download

PDF Word HTML