[英]BMI calculator GUI
我的 BMI 計算器運行但沒有顯示錯誤和結果。 我想使用 JOptionPane + JFrame。 最終遇到了我無法找到的問題。 我需要它作為磅和英寸然后轉換為厘米和公斤。完成之后程序不會與 JOptionPane 一起運行,或者這部分是問題所在
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class ComputeBMI {
public static void main(String[] args) {
new ComputeBMI();
}
{
//create scanner
final int WIDTH = 275;
final int HEIGHT = 100;
JFrame frame;
JPanel panel;
JLabel heightLabel, weightLabel, BMILabel, resultLabel;
JTextField height;
JTextField weight;
JButton calculate;
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 Inch:");
weightLabel = new JLabel("Your weight in Pounds: ");
//create a "this is your BMI" label
BMILabel = new JLabel("Your BMI is ");
//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(5);
//create a JTextField to hold the person's weight in pounds
weight = new JTextField(5);
//create a button to press to calculate BMI
calculate = new JButton("calculate BMI");
//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);
panel.add(height);
//add the weight label and weight textfield to the panel
panel.add(weightLabel);
panel.add(weight);
//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 = null;
frame.pack();
frame.setVisible(true);
}
//*****************************************************************
// 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) {
double pounds;
double inches;
DecimalFormat fmt = new DecimalFormat("0.00");
//prompt user
//create labels for the height and weight textfields
inches =
Double.parseDouble((String)JOptionPane.showInputDialog(null, "Enter height in inches: ", "Height", JOptionPane.QUESTION_MESSAGE, null, null, "Numbers only!"));
pounds =
Double.parseDouble((String)JOptionPane.showInputDialog(null, "Enter weight in pounds: ", "Weight", JOptionPane.QUESTION_MESSAGE, null, null, "Numbers only !"));
//convert measurements
Double weightInKilos = pounds * 0.453592;
Double heightInMeters = inches * 0.0254;
Double bmi = weightInKilos / Math.pow(heightInMeters, 2.0);
// double bmi = weightInKilos / (heightInMeters * heightInMeters);
//display output
JOptionPane.showMessageDialog(null, "Your BMI is: " + fmt.format(bmi));
//interpret BMI
if (bmi < 18.5) {
JOptionPane.showMessageDialog(null, "Underweight");
} else if (bmi >= 18.5 && bmi < 25) {
JOptionPane.showMessageDialog(null, "Normal");
} else if (bmi >= 25 && bmi < 30) {
JOptionPane.showMessageDialog(null, "Overweight");
} else if (bmi >= 30) {
JOptionPane.showMessageDialog(null, "Obese");
} else {
JOptionPane.showMessageDialog(null, " You got to enter a proper number.");
}
}
}
AbstractButton resultLabel;
NumberFormat fmt;
//Put result in result label. Use Double.toString to convert double to string.
public Window frame;
{
}
}
您永遠不會調用display()
來顯示您的框架。 您也不能只設置frame = null
因為接下來的幾行會導致NullPointerException
。
public void display() {
frame.pack();
frame.setVisible(true);
}
然后調用你的display()
方法
//add the panel to the frame
frame.getContentPane().add(panel);
display();
將 setVisible(true) 添加到您的框架中。 而且你必須對高度和寬度做一些事情,因為它在你手動調整大小之前是不可見的。
frame.setVisible(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.