PLEASE HELP ME WITH MY CODE. WHENEVER I INPUT AN INVALID CHARACTER IN THE FIRST TEXT FIELD, THE WHOLE PROGRAM JUST FREEZES. BUT IF I PUT AN INVALID CHARACTER ON THE OTHER TEXT FIELDS IT WORKS JUST FINE. PLEASE HELP ME.
import java.util.InputMismatchException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUIarea extends JFrame {
private JLabel inputradiusc, inputradiuss, inputwidth, inputlength, area1, area2, area3, cl1,cl2,cl3;
private JTextField inputrad1, inputrad2, inputwid, inputleng;
private JButton compute, exit;
private CalculateButtonHandler comhandler;
private ExitButtonHandler exithandler;
private static final int width = 1050;
private static final int length = 500;
public GUIarea() {
inputradiusc = new JLabel ("Enter the radius of the Circle: ", SwingConstants.LEFT );
inputradiuss = new JLabel ("Enter the radius of the Sphere: ", SwingConstants.LEFT );
inputwidth = new JLabel ("Enter the Width of a Rectangle: ", SwingConstants.LEFT );
inputlength = new JLabel ("Enter the Length of a Rectangle: ", SwingConstants.LEFT );
area1 = new JLabel("Area of a Circle: ", SwingConstants.RIGHT);
area2 = new JLabel("Area of a Sphere: ", SwingConstants.RIGHT);
area3 = new JLabel("Area of a Rectangle: ", SwingConstants.RIGHT);
cl1 = new JLabel("");
cl2 = new JLabel("");
cl3 = new JLabel("");
inputrad1 = new JTextField(1);
inputrad2 = new JTextField(1);
inputwid = new JTextField(1);
inputleng = new JTextField(1);
compute = new JButton("Compute");
comhandler = new CalculateButtonHandler();
compute.addActionListener(comhandler);
exit = new JButton("Exit");
exithandler = new ExitButtonHandler();
exit.addActionListener(exithandler);
setTitle("Area Calculator by Miguel Ybera");
Container ybs = getContentPane();
ybs.setLayout(new GridLayout(8, 2));
ybs.add(inputradiusc);
ybs.add(inputrad1);
ybs.add(inputradiuss);
ybs.add(inputrad2);
ybs.add(inputwidth);
ybs.add(inputwid);
ybs.add(inputlength);
ybs.add(inputleng);
ybs.add(area1);
ybs.add(cl1);
ybs.add(area2);
ybs.add(cl2);
ybs.add(area3);
ybs.add(cl3);
ybs.add(compute);
ybs.add(exit);
setSize(width, length);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CalculateButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent asdf) {
double num1 , num2 , num3, num4,
areaofcircle, areaofsphere, areaofrectangle;
int x=1;
do {
try {
num1 = Double.parseDouble(inputrad1.getText());
x=2;
areaofcircle = 3.14*num1*num1;
cl1.setText(""+ String.format("%.2f", areaofcircle));
}
catch(Exception wrong) {
cl1.setText("Invalid");
}
}
while (x ==1);
do {
try {
num2 = Double.parseDouble(inputrad2.getText());
x=2;
areaofsphere = 4*3.14*num2*num2;
cl2.setText(""+ String.format("%.2f", areaofsphere));
}
catch(Exception Exception) {
cl2.setText("Invalid");
}
}
while (x ==1);
do {
try {
num3 = Double.parseDouble(inputwid.getText());
num4 = Double.parseDouble(inputleng.getText());
x=2;
areaofrectangle = num3 *num4;
cl3.setText(""+ String.format("%.2f", areaofrectangle));
}
catch(Exception Exception) {
cl3.setText("Invalid");
}
}
while (x ==1);
}
}
private class ExitButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
public static void main(String[] args) {
GUIarea graph = new GUIarea();
}
}