程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 代碼-Java圖形界面 簡易BMI計算器怎樣設計算法

代碼-Java圖形界面 簡易BMI計算器怎樣設計算法

編輯:編程綜合問答
Java圖形界面 簡易BMI計算器怎樣設計算法

老師出差去了 布置作業做一個簡易BMI計算器
寫了半天才寫出一個圖形界面
不知道怎樣實現鍵盤錄入身高、體重,點擊評估按鈕得到BMI和結果(結果輸出的是胖,瘦,正常三種情況)
代碼、界面如下:
圖片說明
圖片說明

最佳回答:


你的核心問題是如何對按鈕進行響應,需要看下ActionListener的相關方法。

    public class BMI extends JFrame{

    private JLabel lblHeight;
    private JLabel lblWeight;
    private JLabel lblBMI;
    private JLabel lblResult;

    private JButton btnRun;
    private JPanel pnlMain;
    private JTextField txtHeight;
    private JTextField txtWeight;
    private JTextField txtBMI;
    private JTextField txtResult;
    DecimalFormat dformat = new DecimalFormat("#.00");
    public BMI(){
        lblHeight = new JLabel("身高(米/m)");
        txtHeight = new JTextField(10);
        lblWeight = new JLabel("體重(千克/kg)");
        txtWeight = new JTextField(10);
        lblBMI = new JLabel("健康值(BMI)");
        txtBMI = new JTextField(10);
        lblResult = new JLabel("結果");
        txtResult = new JTextField(10);
        btnRun = new JButton("評估");
        pnlMain = new JPanel();
        pnlMain.setLayout(null);
        lblHeight.setBounds(100, 50, 80, 25);
        txtHeight.setBounds(200, 50, 100, 25);
        lblWeight.setBounds(100, 80, 80, 25);
        txtWeight.setBounds(200, 80, 100, 25);
        lblBMI.setBounds(100, 110, 80, 25);
        txtBMI.setBounds(200, 110, 100, 25);
        lblResult.setBounds(100, 170, 80, 25);
        txtResult.setBounds(200, 170, 100, 25);
        btnRun.setBounds(150, 140, 80, 25);

        pnlMain.add(lblHeight);
        pnlMain.add(txtHeight);
        pnlMain.add(lblWeight);
        pnlMain.add(txtWeight);
        pnlMain.add(lblBMI);
        pnlMain.add(txtBMI);
        pnlMain.add(lblResult);
        pnlMain.add(txtResult);
        pnlMain.add(btnRun);
        this.setContentPane(pnlMain);
        setSize(400,300);
        setTitle("健康評估");
        setVisible(true);
        setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        btnRun.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if( txtWeight.getText() == null){
                    JOptionPane.showMessageDialog(null, "身高為空,請輸入一個正值身高!");
                    return;
                }
                if( txtHeight.getText() == null){
                    JOptionPane.showMessageDialog(null, "體重為空,請輸入一個正值體重!");
                    return; 
                }
                double weight = Double.valueOf(txtWeight.getText());
                if(weight <= 0 ){
                    JOptionPane.showMessageDialog(null, "體重為0,請輸入一個正值體重。");
                    return;
                }
                double height = Double.valueOf(txtHeight.getText());
                if(height <= 0 ){
                    JOptionPane.showMessageDialog(null, "身高為0,請輸入一個正值身高。");
                    return;
                }
                double bmi = weight/height/height;
                txtBMI.setText(dformat.format(bmi));
                txtResult.setText(getResult(bmi));
            }

        });
    }
    private String getResult(double bmi) {
        // TODO Auto-generated method stub
        if(bmi < 18.5){
            return "瘦";
        }else if(bmi>=18.5 && bmi < 24){
            return "正常";
        }else{
            return "胖";
        }
    }

    public static void main(String[] args) {
        new BMI();
    }
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved