gpt4 book ai didi

java - BMI计算器图形用户界面

转载 作者:行者123 更新时间:2023-11-30 05:30:31 28 4
gpt4 key购买 nike

我的 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();

关于java - BMI计算器图形用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57657020/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com