Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

BMI calculator/GUI

807601May 1 2008 — edited May 2 2008
Hi! everyone
I am trying to finish the BMI calculator skeleton got from lab

Here is the BMIGUI.java class
//********************************************************************
//  BMIGUI.java     
//
//  Computes body mass index in a GUI.
//********************************************************************

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

public class BMIGUI
{
   private int WIDTH = 300;
   private int HEIGHT = 120;

   private JFrame frame;
   private JPanel panel;
   private JLabel heightLabel, weightLabel, BMILabel, resultLabel;
   private JTextField height, weight;
   private JButton calculate;

   //-----------------------------------------------------------------
   //  Sets up the GUI.
   //-----------------------------------------------------------------
   public BMIGUI()
   {
      frame = new JFrame ("BMI Calculator");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      //create labels for the height and weight textfields 
      heightLabel = new JLabel ("Your height in inches:");
      weightLabel = new JLabel ("Your weight in pounds: ");

      //create a "this is your BMI" label
      BMILabel = new JLabel ("this is your BMI:");
      //create a result label to hold the BMI value
      resultLabel = new JLabel("");

      //create a JTextField to hold the person's height in inches
      height = new JTextField();
      //create a JTextField to hold the person's weight in pounds
      weight = new JTextField ();

      //create a button to press to calculate BMI
      calculate = new JButton ("result");
      //create a BMIListener and make it listen for the button to be pressed
      calculate.addActionListener (new BMIListener());

      //set up the JPanel to go on the JFrame 
      panel = new JPanel();
      panel.setPreferredSize (new Dimension(WIDTH, HEIGHT));
      panel.setBackground (Color.yellow);

      //add the height label and height textfield to the panel
      panel.add(heightLabel);
      //add the weight label and weight textfield to the panel
      panel.add(weightLabel);
      //add the button to the panel
      panel.add(calculate);
      //add the BMI label to the panel
      panel.add(BMILabel);
      //add the label that holds the result to the panel
      panel.add(resultLabel);
      //add the panel to the frame 
      frame.getContentPane().add (panel);
   }

   //-----------------------------------------------------------------
   //  Displays the primary application frame.
   //-----------------------------------------------------------------
   public void display()
   {
      frame.pack();
      frame.show();
   }

   //*****************************************************************
   //  Represents an action listener for the calculate button.
   //*****************************************************************
   private class BMIListener implements ActionListener
   {
      //--------------------------------------------------------------
      //  Compute the BMI when the button is pressed
      //--------------------------------------------------------------
      public void actionPerformed (ActionEvent event)
      {
         String heightText, weightText;
         int heightVal, weightVal;
     double bmi;

     //get the text from the height and weight textfields
     heightText = height;
     weightText = weight;
     //Use Integer.parseInt to convert the text to integer values
     heightVal = Integer.parseInt(heightText);
     weightVal = Interger.parseInt(weightText);
     //Calculate the bmi = 703 * weight in pounds / (height in inches)^2
     double bmi = 0;
     bmi = (703 * (double)weightVal)/(heightVal * heightVal); 
     //Put result in result label.  Use Double.toString to convert double to string.
     resultLabel = bmi.toString();
      }
   }
}
I have trouble to get the text from the height and weight textfields
heightText = height;
     weightText = weight;
The heightText is of type String, but height is not, what should I do?
Thank you
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 30 2008
Added on May 1 2008
19 comments
3,198 views