4

I can't figure out where I am going wrong with this, I have tried to changing a few things but I just can't get the "CalculateButtonHandler to work correctly. Sorry put out all this code but last time I wasn't specific enough :S If someone could point me in the right direction that would be great. Thanks.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// some kind of problem with Calculate button handler (LINE 78 

public class Program5 extends JFrame
{
    // Setting up for the program
    private JLabel side1, side2, side3, str, result;

    private JButton calculate, endProgram;

    private JTextField input1, input2, input3;

    private CalculateButtonHandler calcHandler;
    private EndProgramButtonHandler endHandler;

    private Container pane;

    private static final int WIDTH = 500;
    private static final int HEIGHT = 350;

    // constructor
    public Program5()
    {
        // create labels
        side1 = new JLabel ("Triangle's Longest Side: ", SwingConstants.CENTER);
        side2 = new JLabel ("Triangle's Next Side: ", SwingConstants.CENTER);
        side3 = new JLabel ("Triangle's Last Side: ", SwingConstants.CENTER);
        result = new JLabel ("", SwingConstants.CENTER);
        str = new JLabel ("Is the Triangle a right Triangle?",SwingConstants.CENTER);

        // create text fields
        input1 = new JTextField ("", 60);
        input1.setHorizontalAlignment(JTextField.CENTER);
        input2 = new JTextField ("", 60);
        input2.setHorizontalAlignment(JTextField.CENTER);
        input3 = new JTextField ("", 60);
        input3.setHorizontalAlignment(JTextField.CENTER);

        // create buttons
        calculate = new JButton ("Calculate");
        calcHandler = new CalculateButtonHandler ();
        calculate.addActionListener(calcHandler);

        endProgram = new JButton ("Exit");
        endHandler = new EndProgramButtonHandler();
        endProgram.addActionListener(endHandler);

        // Set title of Window
        setTitle ("Right Triangle Tester");

        //Get Container
        pane = getContentPane();

        // set Layout
        pane.setLayout (new GridLayout (5, 2));

        // place the components in the pane
        pane.add(side1);
        pane.add(input1);
        pane.add(side2);
        pane.add(input2);
        pane.add(side3);
        pane.add(input3);
        pane.add(str);
        pane.add(result);
        pane.add(calculate);
        pane.add(endProgram);

        // set size of the window and display it
        setSize (WIDTH, HEIGHT);
        setVisible (true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
    }

    private class CalculateButtonHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            double num1, num2, num3;
            num1 = Double.parseDouble(side1.getText());
            num2 = Double.parseDouble(side2.getText());
            num3 = Double.parseDouble(side3.getText());

            if ((num1*num1) == ((num2*num2)+(num3*num3)))
            {
                result.setText("YES");
            }
            else
            {
            result.setText("NO");
            }
        }       
    }

    private class EndProgramButtonHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            System.exit(0);
        }
    }

    public static void main(String[] args)  
    {       
        Program5 myObject = new Program5();
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
ChewOnThis_Trident
  • 2,105
  • 2
  • 18
  • 22
  • Please define "work correctly" and "work incorrectly". What do you expect it to do, and what does it do instead? – JB Nizet Feb 13 '12 at 12:49

4 Answers4

2

1) use JFormattedTextField (witt Number instance) rather than plain JTextField, then you couldn't ever to solving parsing Number instance from String,

2) looks like this code parse text from JLabels instead of JTextFields

mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

You are parsing the sideX text which are the JLabels and not your JTextFields. Replace the sideX by inputX in your CalculateButtonHandler and all will be fine

Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
2

In your CalculateButtonHandler class you using JLabel to get input, seems like you need JTextField to get input for that, change that class to this :

private class CalculateButtonHandler implements ActionListener
{
    public void actionPerformed (ActionEvent e)
    {
        double num1, num2, num3;
        num1 = Double.parseDouble(input1.getText());
        num2 = Double.parseDouble(input2.getText());
        num3 = Double.parseDouble(input3.getText());

        if ((num1*num1) == ((num2*num2)+(num3*num3)))
        {
            result.setText("YES");
        }
        else
        {
            result.setText("NO");
        }
    }       
}

And From next time always Schedule a JOB for the Event Dispatcher Thread, while using Swing like in your case your main method must look like this :

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// some kind of problem with Calculate button handler (LINE 78 

public class Program5 extends JFrame
{
    // Setting up for the program
    private JLabel side1, side2, side3, str, result;

    private JButton calculate, endProgram;

    private JTextField input1, input2, input3;

    private CalculateButtonHandler calcHandler;
    private EndProgramButtonHandler endHandler;

    private Container pane;

    private static final int WIDTH = 500;
    private static final int HEIGHT = 350;

    // constructor
    public Program5()
    {
            // create labels
        side1 = new JLabel ("Triangle's Longest Side: ", SwingConstants.CENTER);
        side2 = new JLabel ("Triangle's Next Side: ", SwingConstants.CENTER);
        side3 = new JLabel ("Triangle's Last Side: ", SwingConstants.CENTER);
        result = new JLabel ("", SwingConstants.CENTER);
        str = new JLabel ("Is the Triangle a right Triangle?",SwingConstants.CENTER);


            // create text fields
        input1 = new JTextField ("",60);
        input1.setHorizontalAlignment(JTextField.CENTER);
        input2 = new JTextField ("",60);
        input2.setHorizontalAlignment(JTextField.CENTER);
        input3 = new JTextField ("",60);
        input3.setHorizontalAlignment(JTextField.CENTER);

        // create buttons
        calculate = new JButton ("Calculate");
        calcHandler = new CalculateButtonHandler ();
        calculate.addActionListener(calcHandler);

        endProgram = new JButton ("Exit");
        endHandler = new EndProgramButtonHandler();
        endProgram.addActionListener(endHandler);

        // Set title of Window
        setTitle ("Right Triangle Tester");

        //Get Container
        pane = getContentPane();

        // set Layout
        pane.setLayout (new GridLayout (5,2));

        // place the components in the pane
        pane.add(side1);
        pane.add(input1);
        pane.add(side2);
        pane.add(input2);
        pane.add(side3);
        pane.add(input3);
        pane.add(str);
        pane.add(result);
        pane.add(calculate);
        pane.add(endProgram);

        // set size of the window and display it
        setSize (WIDTH, HEIGHT);
        se1tVisible (true);
        setDefaultCloseOperation (EXIT_ON_CLOSE);       
    }

    private class CalculateButtonHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            double num1, num2, num3;
            num1 = Double.parseDouble(input1.getText());
            num2 = Double.parseDouble(input2.getText());
            num3 = Double.parseDouble(input3.getText());

            if ((num1*num1) == ((num2*num2)+(num3*num3)))
            {
                result.setText("YES");
            }
            else
            {
                result.setText("NO");
            }
        }       
    }

    private class EndProgramButtonHandler implements ActionListener
    {
        public void actionPerformed (ActionEvent e)
        {
            System.exit(0);
        }
    }

    public static void main(String[] args)  
    {
        // Here this is used to attain Concurrency in Swing.
        // So that if there is a need to update the GUI, 
        // that can be done without any difficulty or freezing the GUI.
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                Program5 myObject = new Program5();
            }
        });
    }
}

To know more about what I am talking about read Concurrency in Swing. Very Important thing about Swing is on this Chapter.

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
  • I originally had my calculate as a JTextField, but it still wasn't reading. I don't understand what the "SwingUtilities.invokeLater (new Runnables()) is (I will have to look into that a little later today) But the error I get in eclipse is says something about AWT-EventQue-0 and then goes on to talk about floatingDecimals (I am still figuring out how to debug in eclipse) Have I set up the num1 num2 num3 correctly? – ChewOnThis_Trident Feb 13 '12 at 16:12
  • @ChewOnThis_Trident : Give me 2 mins, I be posting your code with the needed changes, that might can explain you things. – nIcE cOw Feb 13 '12 at 16:27
1

You're also directly comparing doubles. It's very unlikely that you will get this comparison as true. Run this:

     double num1,num2,num3;
     num1=2.8284271247461903;   //== Math.sqrt(8) 
     num2=2.0; num3=2.0;
     if ((num1*num1) == ((num2*num2)+(num3*num3)))
         System.out.println  ("yes");
     else System.out.println  ("no " + num1*num1 + " " + (num2*num2 + num3*num3));

}

wmz
  • 3,645
  • 1
  • 14
  • 22