Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

GUI Form Exercise

Create the following GIU using the Java language, and make sure the program works as intended:

When Submit button is clicked, the name, the surname and the year the user was born are
displayed in a JOptionPane.showMessage Dialog.

When the Clear button is clicked, all fields will be cleared.

Enter Name:

Enter Surname:

Enter Age:

Submit Clear
ANSWER OF THE QUESTION:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

public class GUIForm extends JFrame implements ActionListener {

JLabel nameInfo = new JLabel("Please enter your name : ");


JLabel surnameInfo = new JLabel("Please enter your surname : ");
JLabel ageInfo = new JLabel("Please enter your age : ");
JTextField nameText = new JTextField(25);
JTextField surnameText = new JTextField(25);
JTextField ageText = new JTextField(25);

JButton btnSubmit = new JButton("Submit");


JButton btnClear = new JButton("Clear");

GUIForm(String title){
setTitle(title);
setSize(500,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

add(nameInfo);
add(nameText);
add(surnameInfo);
add(surnameText);
add(ageInfo);
add(ageText);
add(btnSubmit);
add(btnClear);

btnSubmit.addActionListener(this);
btnClear.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == btnSubmit){

String name,surname,ageStr ;
String output="";
int age = 0,yearBorn =0;
int currentYear = Calendar.getInstance().get(Calendar.YEAR);

name = nameText.getText().toString();
surname = surnameText.getText().toString();
ageStr = ageText.getText().toString();

try {

age = Integer.parseInt(ageStr);
yearBorn = currentYear - age;
output = "\n Hello " + name + " " + surname +" \n it seems
like you were born in " + yearBorn;

JOptionPane.showMessageDialog(this, output);

}catch (Exception ex){

JOptionPane.showMessageDialog(this,
"Please enter a number to the age field","Error",
JOptionPane.ERROR_MESSAGE);

System.out.println(ex.getMessage());
}

}
else if (e.getSource() == btnClear){

nameText.setText(""); // empty name text


surnameText.setText("");
ageText.setText("");
nameText.requestFocus(); // starting location of the cursor
}
}
}

You might also like