欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java Swing实现简单的体重指数(BMI)计算器功能示例

程序员文章站 2023-12-09 14:52:21
本文实例讲述了java swing实现简单的体重指数(bmi)计算器功能。分享给大家供大家参考,具体如下: bmi,body mass index,身体质量指数,是用体重...

本文实例讲述了java swing实现简单的体重指数(bmi)计算器功能。分享给大家供大家参考,具体如下:

bmi,body mass index,身体质量指数,是用体重公斤数 除以 身高米数平方得出的,是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。

而本文通过运用java swing实现了一个简单的bmi计算器。虽然现在网页上也有相应的网页应用,但是能够做出这个计算器来,还是有点成就感的,希望自己以后做出更多比这个好的应用。

最终运行效果:

Java Swing实现简单的体重指数(BMI)计算器功能示例

功能:可以选择三个标准:中国、亚洲、who,计算结果稍有不同

计算公式:bmi = weight / (height*height)   即 体重公斤数 除以 身高米数平方

package weightindex;
import javax.swing.*;
import javax.swing.border.emptyborder;
import java.awt.*;
import java.awt.event.*;
import java.text.decimalformat;
import java.util.regex.*;
public class weightindex extends jframe {
 /**
  *
  */
 private static final long serialversionuid = 1l;
 private jpanel contentpane;
 private jlabel titlelabel;
 private jpanel contentpanel;
 private jbutton submitbutton;
 private buttongroup bg;
 private jpanel sexpanel;
 private jradiobutton chinaradio;
 private jradiobutton asiaradio;
 private jradiobutton whoradio;
 private jpanel whpanel;
 private jlabel heightlabel;
 private jlabel weightlabel;
 private jtextfield heighttext;
 private jtextfield weighttext;
 private jpanel consolepanel;
 private jlabel consolelabel;
 private jtextfield consoletext;
 private double weight;
 private double height;
 private double bmi;
 /**
  * launch the application.
  */
 public static void main(string[] args) {
  eventqueue.invokelater(new runnable() {
   public void run() {
    try {
     weightindex frame = new weightindex();
     frame.pack();
     frame.setvisible(true);
    } catch (exception e) {
     e.printstacktrace();
    }
   }
  });
 }
 /**
  * create the frame.
  */
 public weightindex() {
  settitle(" - 身高体重指数计算器v1.0");
  setdefaultcloseoperation(jframe.exit_on_close);
  setbounds(100, 100, 450, 300);
  //主容器
  contentpane = new jpanel();
  contentpane.setborder(new emptyborder(5, 5, 5, 5));
  contentpane.setlayout(new borderlayout(0, 0));
  setcontentpane(contentpane);
  //标题,主容器北
  titlelabel = new jlabel("身高体重指数计算器");
  titlelabel.sethorizontalalignment(swingconstants.center);
  contentpane.add(titlelabel, borderlayout.north);
  //存放选项的panel,主容器中
  contentpanel = new jpanel();
  contentpanel.setlayout(new borderlayout());
  contentpane.add(contentpanel,borderlayout.center);
  //提交按钮,主容器南
  submitbutton = new jbutton("计算");
  contentpane.add(submitbutton, borderlayout.south);
  //存放性别选择的panel,选项北
  bg = new buttongroup();
  sexpanel = new jpanel();
  sexpanel.setlayout(new flowlayout());
  contentpanel.add(sexpanel,borderlayout.north);
  chinaradio = new jradiobutton("中国标准");
  chinaradio.setselected(true);
  asiaradio = new jradiobutton("亚洲标准");
  whoradio = new jradiobutton("who(世界卫生组织)标准");
  bg.add(chinaradio);
  bg.add(asiaradio);
  bg.add(whoradio);
  sexpanel.add(chinaradio);
  sexpanel.add(asiaradio);
  sexpanel.add(whoradio);
  //存放身高体重的panel,选项中
  whpanel = new jpanel();
  whpanel.setlayout(new flowlayout());
  contentpanel.add(whpanel,borderlayout.center);
  heightlabel = new jlabel("身高(米/m):");
  weightlabel = new jlabel("体重(千克/kg):");
  heighttext = new jtextfield(10);
  heighttext.settooltiptext("请输入身高");
  weighttext = new jtextfield(10);
  weighttext.settooltiptext("请输入体重");
  whpanel.add(heightlabel);
  whpanel.add(heighttext);
  whpanel.add(weightlabel);
  whpanel.add(weighttext);
  //结果
  consolepanel = new jpanel();
  consolepanel.setlayout(new flowlayout());
  consolelabel = new jlabel("你的身体质量指数为:");
  consoletext = new jtextfield(28);
  consoletext.seteditable(false);
  consolepanel.add(consolelabel);
  consolepanel.add(consoletext);
  contentpanel.add(consolepanel,borderlayout.south);
  submitbutton.addactionlistener(new actionlistener(){
   public void actionperformed(actionevent e) {
    string hstr = heighttext.gettext();
    string wstr = weighttext.gettext();
    pattern pattern = pattern.compile("^[0-9]+(.[0-9]+)?$");
    matcher hisnum = pattern.matcher(hstr);
    matcher wisnum = pattern.matcher(wstr);
    boolean acc = true;
    if( !hisnum.matches()|| !wisnum.matches()){
      acc = false;
    }
    if(acc)
    {
     height = double.parsedouble(hstr);
     weight = double.parsedouble(wstr);
     bmi = weight / (height*height);
     decimalformat df = new decimalformat("#.0");
     string out = "";
     if(chinaradio.isselected())
     {
      if(bmi<18.5)
       out = "偏瘦,瘦骨嶙峋的,多吃点吧!";
      else if(bmi<23.9)
       out = "正常,棒棒哒!";
      else if(bmi<28)
       out = "偏胖,你胖你就运动减肥!";
      else if(bmi>=28)
       out = "肥胖,现在减肥来的及!";
      else
       out ="外星人派来的你哦,请重新输入!";
     }
     else if(asiaradio.isselected())
     {
      if(bmi<18.5)
       out = "偏瘦,瘦骨嶙峋的,多吃点吧!";
      else if(bmi<22.9)
       out = "正常,棒棒哒!";
      else if(bmi<24.9)
       out = "偏胖,你胖你就运动减肥!";
      else if(bmi<30)
       out = "肥胖,现在减肥来的及!";
      else if(bmi>=30)
       out = "重度肥胖,不是一般人,赶紧减肥吧!";
      else
       out ="外星人派来的你哦,请重新输入!";
     }
     else
     {
      if(bmi<18.5)
       out = "偏瘦,瘦骨嶙峋的,多吃点吧!";
      else if(bmi<24.9)
       out = "正常,棒棒哒!";
      else if(bmi<29.9)
       out = "偏胖,你胖你就运动减肥!";
      else if(bmi<34.9)
       out = "肥胖,现在减肥来的及!";
      else if(bmi<39.9)
       out = "重度肥胖,不是一般人,赶紧减肥吧!";
      else if(bmi>=40)
       out = "极重度肥胖,那可能得去医院看看咯!";
      else
       out ="外星人派来的你哦,请重新输入!";
     }
     consoletext.settext("你的指数为:"+df.format(bmi)+",你的健康状况为:"+out);
    }
   }
  });
 }
}

更多关于java算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。