ある参考書のコードです。
java
1import javafx.application.Application; 2import javafx.geometry.Insets; 3import javafx.geometry.Pos; 4import javafx.scene.Scene; 5import javafx.scene.control.Button; 6import javafx.scene.control.Label; 7import javafx.scene.control.TextField; 8import javafx.scene.layout.HBox; 9import javafx.scene.layout.VBox; 10import javafx.stage.Stage; 11 12public class aaaa extends Application { 13 14 public static void main(String[] args) { 15 launch(args); 16 } 17 18 TextField textFieldH = new TextField(); 19 TextField textFieldW = new TextField(); 20 Label lblBMI = new Label("BMI"); 21 22 @Override 23 public void start(Stage stage) throws Exception { 24 25 stage.setTitle("BMI"); 26 stage.setWidth(200); 27 stage.setHeight(155); 28 29 Label lblH = new Label(" 身長(cm):"); 30 textFieldH.setPrefWidth(80); 31 32 HBox boxH = new HBox(); 33 boxH.getChildren().addAll(lblH, textFieldH); 34 35 36 Label lblW = new Label(" 体重(kg):"); 37 textFieldW.setPrefWidth(80); 38 39 HBox boxW = new HBox(); 40 boxW.getChildren().addAll(lblW, textFieldW); 41 42 43 Button btnGo = new Button("計算"); 44 btnGo.setOnMouseClicked(event -> updateValue(stage)); 45 46 VBox root = new VBox(); 47 root.setAlignment(Pos.TOP_CENTER); 48 root.setPadding(new Insets(5, 5, 5, 5)); 49 root.setSpacing(5.0); 50 root.getChildren().addAll(boxH, boxW, btnGo, lblBMI); 51 52 stage.setScene(new Scene(root)); 53 stage.show(); 54 } 55 56 void updateValue(Stage stage) 57 { 58 String sh = textFieldH.getText(); 59 String sw = textFieldW.getText(); 60 double h = Double.parseDouble( sh.trim() ); 61 double w = Double.parseDouble( sw.trim() ); 62 double bmi = 10000.0 * w / (h * h); 63 lblBMI.setText(String.format("BMI=%5.2f", bmi)); 64 } 65}
上記が全体のコードなのですが
Java
1Button btnGo = new Button("計算"); 2 btnGo.setOnMouseClicked(event -> updateValue(stage));
と
Java
1void updateValue(Stage stage) 2 { 3 String sh = textFieldH.getText(); 4 String sw = textFieldW.getText(); 5 double h = Double.parseDouble( sh.trim() ); 6 double w = Double.parseDouble( sw.trim() ); 7 double bmi = 10000.0 * w / (h * h); 8 lblBMI.setText(String.format("BMI=%5.2f", bmi));
のコードでstageが引数として使われている理由がわかりません。
一応、
Java
1Button btnGo = new Button("計算"); 2 btnGo.setOnMouseClicked(event -> updateValue());
Java
1void updateValue() 2 { 3 String sh = textFieldH.getText(); 4 String sw = textFieldW.getText(); 5 double h = Double.parseDouble( sh.trim() ); 6 double w = Double.parseDouble( sw.trim() ); 7 double bmi = 10000.0 * w / (h * h); 8 lblBMI.setText(String.format("BMI=%5.2f", bmi));
としてコンパイル、実行してみたのですが、問題なく意図した動きをしてくれました。
なぜstageが引数として使われていたのか、その意図は何かがわかりませんでした。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/29 03:55
2017/09/29 03:59