この記事では、Java SwingのSimple Body Mass Index(BMI)電卓機能について説明します。次のように、参照のために共有してください。
BMI、ボディマス指数は、キログラムの重量をメートルの高さで割ったものから派生しています。現在、体の体重を測定するための一般的な基準と、それが健康であるかどうかです。
この記事では、Java Swingを使用して、簡単なBMI計算機を実装しています。現在、Webページには対応するWebアプリケーションがありますが、この計算機を作成できることはまだ少し充実しています。将来、これよりも優れたアプリケーションを作成できることを願っています。
最終的な実行効果:
機能: 3つの基準を選択できます:中国、アジア、およびWHO、および計算結果はわずかに異なります。
計算式: BMI =重量 /(高さ*高さ*)、つまり、重量キログラムを高さの正方形で割った
パッケージweightIndex; Import javax.swing。*; Import javax.swing.border.emptyborder; import java.awt。*; Import java.awt.event。プライベートJPanel ContentSpane;プライベートJlabel TitleLabel;プライベートJPanel ContentPanel;プライベートjbutton submitbutton;プライベートボトングループBG;プライベートjpanel sexpanel;プライベートJradiobutton Chinaradio;プライベートJradiobutton asiaradio;プライベートJradiobutton Whoradio;プライベートjpanel whpanel;プライベートJlabel heightlabel;プライベートJlabel weightlabel;プライベートjtextfield heighttext;プライベートjtextfield weighttext;プライベートJPanel Consolepanel;プライベートJlabel Consolelabel;プライベートJTextfield ConsoleText;プライベートダブルウェイト;プライベートダブルハイト;プライベートダブルBMI; /***アプリケーションを起動します。 */ public static void main(string [] args){eventqueue.invokelater(new runnable(){public void run(){try {weightindex frame = new heightIndex(); frame.pack(); frame.setVisible(true);} cated(Expection e){e.printstrace();}}}}}}}); } /***フレームを作成します。 */ public weightIndex(){settitle( "wulin.com-高さと重量指数計算機V1.0"); setDefaultCloseoperation(jframe.exit_on_close);セットバウンド(100、100、450、300); //メインコンテナContentSpane = new JPanel(); contentSpane.setborder(新しいemptyborder(5、5、5、5)); contentSpane.setLayout(new borderlayout(0、0)); setContentPane(contentSpane); //タイトル、メインコンテナNorth TitleLabel = new Jlabel( "Height and Weight Index Calculator"); titlelabel.sethorizontalAlignment(SwingConstants.Center); contentSpane.add(titlelabel、borderlayout.north); //オプションのパネルを保存する、メインコンテナcontentpanel = new JPanel(); contentPanel.setLayout(new borderlayout()); contentSpane.add(contentSpanel、borderlayout.center); //送信ボタン、メインコンテナサウスSubmitButton = new JButton( "Calculation"); contentSpane.add(submitbutton、borderlayout.south); //性別選択パネルを保存、オプションノース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(世界保健機関)標準"); Bg.Add(Chinaradio); bg.add(asiaradio); bg.add(whoradio); sexpanel.add(chinaradio); sexpanel.add(asiaradio); sexpanel.add(whoradio); //オプションで高さとウェイトパネルを保存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( "Heightを入力してください"); weighttext = new JTextfield(10); weighttext.settooltiptext( "重量を入力してください"); whpanel.add(heightlabel); whpanel.add(heightText); whpanel.add(weightlabel); whpanel.add(weighttext); // result consolepanel = new JPanel(); consolepanel.setLayout(new FlowLayout()); consolelabel = new Jlabel( "あなたのボディマス指数は:"); consoletext = new JTextfield(28); consoletext.setedable(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.compile( "^[0-9]+(。[0-9]+)?パターン(WSTR); hisnum.matches()弦楽out =「エイリアンによって送られてください!」 「今、体重を減らす時が来ました> = 30) (bmi <29.9)out "あなたが今体重を減らすことができます!」 else out = "あなたはエイリアンからあなたを送ります、再び入力してください!" consoletext.settext( "+df.format(bmi)+"、そしてあなたの健康は "+out); }}Javaアルゴリズムの詳細については、このサイトに興味のある読者は、「Javaデータ構造とアルゴリズムのチュートリアル」、「Java操作DOMノードのヒントの要約」、「Javaファイルの要約およびディレクトリ操作のヒント」、「Java Cache操作のヒントの要約」というトピックを見ることができます。
この記事がみんなのJavaプログラミングに役立つことを願っています。