Java Swing實(shí)現(xiàn)簡(jiǎn)單的體重指數(shù)(BMI)計(jì)算器功能示例
本文實(shí)例講述了Java Swing實(shí)現(xiàn)簡(jiǎn)單的體重指數(shù)(BMI)計(jì)算器功能。分享給大家供大家參考,具體如下:
BMI,Body Mass Index,身體質(zhì)量指數(shù),是用體重公斤數(shù) 除以 身高米數(shù)平方得出的,是目前國(guó)際上常用的衡量人體胖瘦程度以及是否健康的一個(gè)標(biāo)準(zhǔn)。
而本文通過運(yùn)用Java Swing實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的BMI計(jì)算器。雖然現(xiàn)在網(wǎng)頁(yè)上也有相應(yīng)的網(wǎng)頁(yè)應(yīng)用,但是能夠做出這個(gè)計(jì)算器來,還是有點(diǎn)成就感的,希望自己以后做出更多比這個(gè)好的應(yīng)用。
最終運(yùn)行效果:
功能:可以選擇三個(gè)標(biāo)準(zhǔn):中國(guó)、亞洲、WHO,計(jì)算結(jié)果稍有不同
計(jì)算公式:BMI = weight / (height*height) 即 體重公斤數(shù) 除以 身高米數(shù)平方
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("腳本之家 - 身高體重指數(shù)計(jì)算器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); //標(biāo)題,主容器北 titleLabel = new JLabel("身高體重指數(shù)計(jì)算器"); titleLabel.setHorizontalAlignment(SwingConstants.CENTER); contentPane.add(titleLabel, BorderLayout.NORTH); //存放選項(xiàng)的panel,主容器中 contentPanel = new JPanel(); contentPanel.setLayout(new BorderLayout()); contentPane.add(contentPanel,BorderLayout.CENTER); //提交按鈕,主容器南 submitButton = new JButton("計(jì)算"); contentPane.add(submitButton, BorderLayout.SOUTH); //存放性別選擇的panel,選項(xiàng)北 bg = new ButtonGroup(); sexPanel = new JPanel(); sexPanel.setLayout(new FlowLayout()); contentPanel.add(sexPanel,BorderLayout.NORTH); ChinaRadio = new JRadioButton("中國(guó)標(biāo)準(zhǔn)"); ChinaRadio.setSelected(true); AsiaRadio = new JRadioButton("亞洲標(biāo)準(zhǔn)"); WHORadio = new JRadioButton("WHO(世界衛(wèi)生組織)標(biāo)準(zhǔn)"); bg.add(ChinaRadio); bg.add(AsiaRadio); bg.add(WHORadio); sexPanel.add(ChinaRadio); sexPanel.add(AsiaRadio); sexPanel.add(WHORadio); //存放身高體重的panel,選項(xiàng)中 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("請(qǐng)輸入身高"); weightText = new JTextField(10); weightText.setToolTipText("請(qǐng)輸入體重"); whPanel.add(heightLabel); whPanel.add(heightText); whPanel.add(weightLabel); whPanel.add(weightText); //結(jié)果 consolePanel = new JPanel(); consolePanel.setLayout(new FlowLayout()); consoleLabel = new JLabel("你的身體質(zhì)量指數(shù)為:"); 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 = "偏瘦,瘦骨嶙峋的,多吃點(diǎn)吧!"; else if(BMI<23.9) out = "正常,棒棒噠!"; else if(BMI<28) out = "偏胖,你胖你就運(yùn)動(dòng)減肥!"; else if(BMI>=28) out = "肥胖,現(xiàn)在減肥來的及!"; else out ="外星人派來的你哦,請(qǐng)重新輸入!"; } else if(AsiaRadio.isSelected()) { if(BMI<18.5) out = "偏瘦,瘦骨嶙峋的,多吃點(diǎn)吧!"; else if(BMI<22.9) out = "正常,棒棒噠!"; else if(BMI<24.9) out = "偏胖,你胖你就運(yùn)動(dòng)減肥!"; else if(BMI<30) out = "肥胖,現(xiàn)在減肥來的及!"; else if(BMI>=30) out = "重度肥胖,不是一般人,趕緊減肥吧!"; else out ="外星人派來的你哦,請(qǐng)重新輸入!"; } else { if(BMI<18.5) out = "偏瘦,瘦骨嶙峋的,多吃點(diǎn)吧!"; else if(BMI<24.9) out = "正常,棒棒噠!"; else if(BMI<29.9) out = "偏胖,你胖你就運(yùn)動(dòng)減肥!"; else if(BMI<34.9) out = "肥胖,現(xiàn)在減肥來的及!"; else if(BMI<39.9) out = "重度肥胖,不是一般人,趕緊減肥吧!"; else if(BMI>=40) out = "極重度肥胖,那可能得去醫(yī)院看看咯!"; else out ="外星人派來的你哦,請(qǐng)重新輸入!"; } consoleText.setText("你的指數(shù)為:"+df.format(BMI)+",你的健康狀況為:"+out); } } }); } }
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Java Swing編寫一個(gè)簡(jiǎn)單的計(jì)算器軟件
- 基于Java swing組件實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
- java實(shí)現(xiàn)的計(jì)算器功能示例【基于swing組件】
- Java swing實(shí)現(xiàn)的計(jì)算器功能完整實(shí)例
- java實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器界面
- Java實(shí)現(xiàn)簡(jiǎn)單圖形界面計(jì)算器
- java圖形界面之加法計(jì)算器
- Java簡(jiǎn)易計(jì)算器程序設(shè)計(jì)
- java實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- java swing實(shí)現(xiàn)簡(jiǎn)單計(jì)算器界面
相關(guān)文章
SpringBoot自動(dòng)裝配原理詳細(xì)解析
這篇文章主要介紹了SpringBoot自動(dòng)裝配原理詳細(xì)解析,一個(gè)對(duì)象交給Spring來管理的三種方式 @Bean @Compoment @Import,2024-01-01
@Bean主要在@Configuration中,通過方法進(jìn)行注入相關(guān)的Bean,@Compoent與@Service歸為一類,在類上加注入對(duì)應(yīng)的類,需要的朋友可以參考下Java虛擬機(jī)JVM性能優(yōu)化(三):垃圾收集詳解
這篇文章主要介紹了Java虛擬機(jī)JVM性能優(yōu)化(三):垃圾收集詳解,本文講解了眾多的JVM垃圾收集器知識(shí)點(diǎn),需要的朋友可以參考下2014-09-09簡(jiǎn)談java并發(fā)FutureTask的實(shí)現(xiàn)
這篇文章主要介紹了簡(jiǎn)談java并發(fā)FutureTask的實(shí)現(xiàn),FutureTask都是用于獲取線程執(zhí)行的返回結(jié)果。文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06在CentOS系統(tǒng)中檢測(cè)Java安裝及運(yùn)行jar應(yīng)用的方法
這篇文章主要介紹了在CentOS系統(tǒng)中檢測(cè)Java安裝及運(yùn)行jar應(yīng)用的方法,同樣適用于Fedora等其他RedHat系的Linux系統(tǒng),需要的朋友可以參考下2015-06-06