Java Discussion Forums

There's no such thing as a stupid question. Ask. Discuss. Learn.

Java Gui problem help

Hi im having a problem with my code. Every time i place and invalid character on the first text field then press calculate it just freezes. But if I place them on the 2nd and 3rd text field it works just fine here is my code

Miguel Ybera 5 months ago
  • Miguel Ybera

    import javax.swing.;
    import java.awt.
    ;
    import java.awt.event.;
    public class GUIarea extends JFrame {
    private JLabel inputradiusc, inputradiuss, inputwidth, inputlength, area1, area2, area3, colon1,colon2,colon3;
    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);
    colon1 = new JLabel("");
    colon2 = new JLabel("");
    colon3 = 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(colon1);
    ybs.add(area2);
    ybs.add(colon2);
    ybs.add(area3);
    ybs.add(colon3);
    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
    num1num1;
    colon1.setText(""+ String.format("%.2f", areaofcircle));
    }
    catch(Exception Exception) {
    colon1.setText("Invalid Input");
    }
    }
    while (x ==1);
    do {
    try {
    num2 = Double.parseDouble(inputrad2.getText());
    x=2;
    areaofsphere = 4
    3.14num2num2;
    colon2.setText(""+ String.format("%.2f", areaofsphere));
    }
    catch(Exception Exception) {
    colon2.setText("Invalid Input");
    }
    }
    while (x ==1);
    do {
    try {
    num3 = Double.parseDouble(inputwid.getText());
    num4 = Double.parseDouble(inputleng.getText());
    x=2;
    areaofrectangle = num3 *num4;
    colon3.setText(""+ String.format("%.2f", areaofrectangle));
    }
    catch(Exception Exception) {
    colon3.setText("Invalid Input");
    }
    }
    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();
    }

    }

  • Alan Rogger

    Same thing I have been facing and I need this to be fixed. I noticed that my Skype Mic Not Working and the GUI failed to load. Please help me to fix the problem.


  • Whitelisted Links