[Java] 简单运用javafx计算BMI →→→→→进入此内容的聊天室

来自 , 2019-06-26, 写在 Java, 查看 95 次.
URL http://www.code666.cn/view/0e3a37aa
  1. package Pane;
  2.  
  3. import javafx.application.Application;
  4. import javafx.stage.Stage;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.control.TextField;
  8. import javafx.scene.control.Label;
  9. import javafx.scene.layout.GridPane;
  10.  
  11.  
  12. public class javaFXBMI extends Application{
  13.     private TextField tfHeight = new TextField();
  14.     private TextField tfWeight = new TextField();
  15.     private TextField tfBMI = new TextField();
  16.     private TextField tfStatus = new TextField();
  17.     private Button btCalculate = new Button("计算BMI");
  18.     public void start(Stage primaryStage){
  19.         GridPane bmiPane = new GridPane();  
  20.         bmiPane.add(new Label("体重(公斤) "),0,0);
  21.         bmiPane.add(tfWeight,1,0);
  22.         bmiPane.add(new Label("身高(米)   "),0,1);
  23.         bmiPane.add(tfHeight,1,1);
  24.         bmiPane.add(new Label("BMI        "),0,2);
  25.         bmiPane.add(tfBMI,1,2);
  26.         bmiPane.add(new Label("体重状况   "),0,3);
  27.         bmiPane.add(tfStatus,1,3);
  28.         bmiPane.add(btCalculate,1,4);
  29.                      
  30.         tfBMI.setEditable(false);      
  31.         tfStatus.setEditable(false);        
  32.          
  33.         btCalculate.setOnAction(e->{
  34.             double weight = Double.parseDouble(tfWeight.getText());
  35.             double height = Double.parseDouble(tfHeight.getText());
  36.  
  37.                          double bmi = weight/(height * height);                          
  38.             tfBMI.setText(bmi + "");
  39.  
  40.                          if ((bmi >= 18.5)&&(bmi < 25))  
  41.                             tfStatus.setText("Normal");
  42.                          else
  43.                             tfStatus.setText("Abnormal");  
  44.         });    
  45.          
  46.         Scene scene = new Scene(bmiPane,250,120);
  47.         primaryStage.setScene(scene);
  48.         primaryStage.setTitle("BMI计算");
  49.         primaryStage.show();
  50.     }
  51.     public static void main(String[] args){
  52.         Application.launch(args);
  53.       }    
  54. }

回复 "简单运用javafx计算BMI"

这儿你可以回复上面这条便签

captcha