Ideal Weight Calculator
Code Highlights:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
JTextField txtWeight;
static JSlider heightScale;
ButtonGroup btnGroup;
JRadioButtonMenuItem btnMale;
JRadioButtonMenuItem btnFemale;
public void calculate_weight()
{
String genderValue;
if(btnMale.isSelected()==true)
{
genderValue = btnMale.getActionCommand();
}
else
{
genderValue = btnFemale.getActionCommand();
}
Integer userHeight = heightScale.getValue();
Integer heightSqr = (userHeight*userHeight);
String idealWeight = Integer.toString(heightSqr/Integer.parseInt(genderValue));
txtWeight.setText(idealWeight);
}
Attachment
I hope this post ideal weight calculator will help java beginners to understand the JSlider, Radio, Labels and Listeners usage. If any query you have regarding this, must ask.
Last updated: March 19, 2014