import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.layout.HBox; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.control.Label; import javafx.stage.Stage; public class AddTime2 extends Application { public void start(Stage stage) { stage.setTitle("時間の計算"); TextField input = new TextField(""); // e.g. 2:45 1:25 3:34 2:47 0:24 input.setPrefColumnCount(16); Label output = new Label("00:00"); output.setAlignment(Pos.BASELINE_RIGHT); input.setOnAction(e -> { String[] args = input.getText().split("\\s+"); int[] t = {0, 0}; for (String s : args) { String[] stime = s.split(":"); t = addTime(t, new int[] { Integer.parseInt(stime[0]), Integer.parseInt(stime[1]) }); // addTimeの呼出し前にその引数に入っていた配列は不要となる。 // あとでGCされる。 } output.setText(String.format("%02d:%02d", t[0], t[1])); }); HBox root = new HBox(); root.setAlignment(Pos.CENTER); root.getChildren().addAll(input, new Label(" の和は "), output, new Label(" です。")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } private static int[] addTime(int[] t1, int[] t2) { // 時間の足し算を関数として定義する。 int[] t3 = { t1[0] + t2[0], t1[1] + t2[1] }; // 時間を大きさ 2の配列で表す。 if (t3[1] >= 60) { // 繰り上がりの処理 t3[0]++; t3[1] -= 60; } return t3; // 新しい配列を返す。 } public static void main(String... args) { launch(args); } }