public class BMI extends JFrame implements ActionListener {
private static final JButton JButton =
null;
private JFrame frame;
private JPanel panel;
private JLabel heightLabel, weightLabel,
BMILabel;
private JTextField height, weight, result;
private JButton calculate;
String Height, Weight;
double hght,wght, BMI;
static String output = "Results";
static int jopIcon =
JOptionPane.QUESTION_MESSAGE;
boolean bFlag = true;
public BMI() {
frame = new
JFrame("BMI Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
heightLabel = new
JLabel("Your height in inches:");
weightLabel = new
JLabel("Your weight in lbs: ");
BMILabel = new
JLabel("Your BMI is ");
result = new
JTextField("");
height = new
JTextField(1);
weight = new
JTextField(1);
calculate = new
JButton("Calculate BMI");
panel = new
JPanel();
panel.add(heightLabel);
panel.add(height);
panel.add(weightLabel);
panel.add(weight);
panel.add(BMILabel);
panel.add(result);
panel.add(calculate);
frame.getContentPane().add(panel);
JPanel p1 = new
JPanel();
panel.setLayout(new
GridLayout(4, 1));
add(p1,
BorderLayout.SOUTH);
calculate.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,
400);
frame.setVisible(true);
}
public String getDateTime() {
DateFormat dateFormat =
new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new
Date();
return
dateFormat.format(date);
}
public void calculateBMI(double wght, hght
number2) {
try {
BMI = wght*703 / ((hght) ^ 2);
} catch
(NumberFormatException nfe) {
output += "\n\n Whoa! Input error: must enter valid
integers";
jopIcon = JOptionPane.ERROR_MESSAGE;
}
}
public void calculate() {
Height =
height.getText();
Weight =
weight.getText();
try {
hght = Double.parseDouble(Height);
wght = Double.parseDouble(Weight);
calculateBMI(hght, wght);
} finally {
if (BMI >= 27.5) {
output += "\n\n You're UnHealthy.";
} else if (BMI <= 23 || BMI < 27.4) {
output += "\n\n You're in the Moderate Risk zone. ";
} else if (BMI <= 18.5 || BMI < 22.9) {
output += " You're Healthy. ";
} else if (BMI < 18.4) {
output += "\n\n You really need to start eating more. ";
}
}
}
output:
your height in inches:64
your weight in lbs:150
your BMI is:25
You're in the Moderate Risk zone
Java program Build the interface: A BMI calculator First: add a Layered Pane; drag it from...