This article describes the Java Swing's simple body mass index (BMI) calculator function. Share it for your reference, as follows:
BMI, Body Mass Index, is derived from the weight of kilograms divided by the height of meters square. It is currently a common standard for measuring the body's weight and whether it is healthy.
This article implements a simple BMI calculator by using Java Swing. Although there are corresponding web applications on the web page now, it is still a bit fulfilling to be able to make this calculator. I hope I can make more good applications than this in the future.
Final running effect:
Function: Three criteria can be selected: China, Asia, and WHO, and the calculation results are slightly different.
Calculation formula: BMI = weight / (height*height) that is, the weight kilograms divided by the height meter square
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("Wulin.com - Height and Weight Index Calculator v1.0"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); //Main container contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); contentPane.setLayout(new BorderLayout(0, 0)); setContentPane(contentPane); //Title, main container north titleLabel = new JLabel("Height and Weight Index Calculator"); titleLabel.setHorizontalAlignment(SwingConstants.CENTER); contentPane.add(titleLabel, BorderLayout.NORTH); //Storing the panel of options, main container contentPanel = new JPanel(); contentPanel.setLayout(new BorderLayout()); contentPane.add(contentPanel,BorderLayout.CENTER); //Submit button, main container south submitButton = new JButton("calculation"); contentPane.add(submitButton, BorderLayout.SOUTH); //Storage the gender-selected panel, option north bg = new ButtonGroup(); sexPanel = new JPanel(); sexPanel.setLayout(new FlowLayout()); contentPanel.add(sexPanel,BorderLayout.NORTH); ChinaRadio = new JRadioButton("Chinese Standard"); ChinaRadio.setSelected(true); AsiaRadio = new JRadioButton("Asia Standard"); WHORadio = new JRadioButton("WHO (World Health Organization) Standard"); bg.add(ChinaRadio); bg.add(AsiaRadio); bg.add(WHORadio); sexPanel.add(ChinaRadio); sexPanel.add(AsiaRadio); sexPanel.add(WHORadio); //Storage the height and weight panel, in the options whPanel = new JPanel(); whPanel.setLayout(new FlowLayout()); contentPanel.add(whPanel,BorderLayout.CENTER); heightLabel = new JLabel("Height (meter/m):"); weightLabel = new JLabel("Weight (kg/kg):"); heightText = new JTextField(10); heightText.setToolTipText("Please enter height"); weightText = new JTextField(10); weightText.setToolTipText("Please enter weight"); whPanel.add(heightLabel); whPanel.add(heightText); whPanel.add(weightLabel); whPanel.add(weightText); //Result consolePanel = new JPanel(); consolePanel.setLayout(new FlowLayout()); consoleLabel = new JLabel("Your body mass index is:"); 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 = "Skinny, skinny, eat more!"; else if(BMI<23.9) out = "Normal, great!"; else if(BMI<28) out = "If you are fat, you should exercise and lose weight!"; else if(BMI>=28) out = "Obacteria, you can lose weight now!"; else out ="You sent by aliens, please re-enter!"; } else if(AsiaRadio.isSelected()) { if(BMI<18.5) out = "Skinny, skinny, eat more!"; else if(BMI<22.9) out = "Normal, great!"; else if(BMI<24.9) out = "If you are fat, you should exercise and lose weight!"; else if(BMI<30) out = "Obacteria, it's time to lose weight now!"; else if(BMI>=30) out = "Severely obese, not ordinary people, lose weight now!"; else out ="You sent by aliens, please re-enter!"; } else { if(BMI<18.5) out = "Small, thin, skinny, eat more!"; else if(BMI<24.9) out = "Normal, great!"; else if(BMI<29.9) out = "If you are fat, you can exercise to lose weight now!"; else if(BMI<34.9) out = "Obacteria, you can lose weight now!"; else if(BMI<39.9) out = "Severe obesity is not an ordinary person, so lose weight now!"; else if(BMI>=40) out = "Extremely obese, you may have to go to the hospital for a look!"; else out ="Your sends you from an alien, please re-enter!"; } consoleText.setText("Your index is: "+df.format(BMI)+", and your health is: "+out); } } }); }}For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.