The GUI has three widgets: a label, an entry, and a button. The label prompts th
ID: 3669086 • Letter: T
Question
The GUI has three widgets: a label, an entry, and a button. The label prompts the user to enter his/her year of birth. When the user enters a year into the entry and presses the button, the GUI calculates the age of the user and displays it in a dialog box. When the user dismisses the dialog box, the GUI clears the entry and goes back to waiting for the next interaction with the user.
The constructor for the GUI is provided in the exam template file. It initializes a Tk window to hold the widgets and calls the make_widgets() method. The make_widgets() method has also been provided. It creates the Label, Button, and Entry and places them into the main window.
Your task for this question is to write the event handler for the Button. The method retrieves whatever the user has typed into the Entry and tries to convert it to an integer. If that conversion is successful and the age entered is positive (>0) it uses localtime() to get the current year and use it to calculate the age and displays it via a showinfo box. If the uear entered is an integer but is not positive (<= 0) it displays an error message in a showinfo box. If the year cannot be converted to an integer, it also displays an error message in a showinfo box. Regardless of what the user types, the Entry is cleared as the last thing that the event handler does.
Suppose that the user enters a valid year as follows:
If the user then presses the “Submit” button, the following dialog box appears. Since the number is chosen randomly, your value may differ from the one I display:
After the user dismisses the box, the entry is cleared as follows:
If the user enters an invalid age an error message is shown in a dialog box. One example of an invalid year is one that is not positive:
The user should see:
The user could also give a value that does not correspond to an integer:
In either of that case, the following dialog box displays:
Once the error dialog box is dismissed the erroneous entry is cleared to allow the user to try again:
Explanation / Answer
Comments added
So here is the button action handler
-------------------------------------------------------------------------------------------------------------------------------
public void actionPerformed(ActionEvent ae)
{
//declaring year variable
int year;
//checking event is from our textbox or not
if(ae.getSource() == yearButton)
{
//converting string to int
try{
year = Integer.parseInt(yearText.getText());
//checking for negative year or not
if(year<0){
JOptionPane.showMessageDialog(null,"please enter a positive number");
//clearing text field
yearText.setText("");
}
else if(year>0){
//getting current year using calender unstance
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
int age = currentYear-year;
//printing result
JOptionPane.showMessageDialog(null,"You are "+age+" years old");
//clearing text field
yearText.setText("");
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"please enter a valid number");
//clearing text field
yearText.setText("");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.