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 Factorial extends Application { public void start(Stage stage) { stage.setTitle("Change Color"); TextField input = new TextField("0"); input.setPrefColumnCount(3); Label output = new Label("1"); output.setPrefWidth(96); output.setAlignment(Pos.BASELINE_RIGHT); input.setOnAction(e -> { try { int n = Integer.parseInt(input.getText()); output.setText(" " + factorial(n)); } catch (NumberFormatException ex) { input.setText("数値!"); } }); 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 factorial(int n) { // factorial -- 階乗のこと int r = 1; for (; n > 0; n--) { r *= n; } return r; } public static void main(String... args) { launch(args); } }