JAVA program to calculate age, using GUI.

This program calculates the age. Input is taken from the user through a Graphical User Interface (GUI). We will be using the following components in our program.


Step 1: Creating a Frame
First thing we do is, create a frame. A frame is nothing but the actual window that you’ll see when the program is executed.
Frame is initialized at line 23:

After initializing our frame, we set is size and location on the screen. This is done at line 26:

By default, the frame’s visibility is set to false, i.e the frame is not visible. So we have to set its visibility to true. Done at line 24:

Next thing we do is set LayOut for the frame, we have put a FlowLayout in this program.

Step 2: Initialize other components.

After finishing the frame, we initialize Buttons, TextField and Label. This is done from line 13 till 22.

Step 3: Add Components to the frame.
After initializing frame and other components, we add those components to the frame. Syntax for doing this is:

Step 4: Add ActionListeners to the frame.

This is the most important part of the program. ActionListeners tell the program that an event has occurred, for example a button is clicked.
We add ActionListener to all the three buttons, so that our program reacts accordingly when a button is clicked.

After adding ActionListener, we create classes which will handle the events. In this program, we have created Class Clear and Exit to perform the required task.

Step 5: Calculate age and display the result.

Whenever the Submit button is clicked, actionPerformed class stores the user entered data in the equivalent variables.
Note: We have put the code in a try block so that we can catch the exception if user doesn’t input a valid date. (If user enters a String or Invalid month.)

After collecting data from the TextFields, our next task will be to parse them into int so we can do the necessary calculations.
CalculateAge() :
The objective of this function is to calculate age based on users input. We first find out current date,month and year from the system using in-built class Date found in

That was just a brief summary of the program. If you have any questions, feel free to ask.


Ouput for age calculator

Tested in Eclipse and Command Line.

The following two tabs change content below.

2 thoughts on “JAVA program to calculate age, using GUI.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">