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

08-05 13:26