首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BMI计算器GUI

BMI计算器GUI
EN

Stack Overflow用户
提问于 2019-08-26 19:26:39
回答 2查看 412关注 0票数 1

我的BMI计算器运行了,但没有显示任何错误和结果。我想使用JOptionPane + JFrame。最终遇到了我找不到的问题。我需要它的磅和英寸,然后转换成厘米和kg.After做了它的程序不能与JOptionPane一起运行,也许这部分是问题

代码语言:javascript
复制
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;

  {
  }

}
EN

回答 2

Stack Overflow用户

发布于 2019-08-26 19:39:41

您从不调用display()来显示您的框架。此外,您不能只设置frame = null,因为下面的几行代码将导致NullPointerException

代码语言:javascript
复制
public void display() {
  frame.pack();
  frame.setVisible(true);
}

然后调用您的display()方法

代码语言:javascript
复制
//add the panel to the frame
frame.getContentPane().add(panel);
display();
票数 1
EN

Stack Overflow用户

发布于 2019-08-26 19:35:17

将setVisible(true)添加到框架中。此外,您还必须对高度和宽度做一些操作,因为在手动调整大小之前,它是不可见的。

代码语言:javascript
复制
frame.setVisible(true);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57657020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档