山傘のプログラミング勉強日記

プログラミングに関する日記とどうでもよい雑記からなるブログです。

JavaFX で電卓を作る その1

JavaFXの勉強のために電卓を作ろうと思います。機能は四則演算で、ボタンの数は16個になります。今回は、ガワだけを作成しました。


f:id:yamakasa3:20180302023203p:plain
電卓

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class Calculator extends Application{

	@Override
	public void start(Stage stage) throws Exception{
		stage.setTitle("MyCalculator");
		stage.setWidth(340);
		stage.setHeight(400);

		Label label = new Label();
		label.setText("数字");
		label.setFont(new Font(100));

		//数字のボタン0-9
		Button btn0 = new Button("0");
		Button btn1 = new Button("1");
		Button btn2 = new Button("2");
		Button btn3 = new Button("3");
		Button btn4 = new Button("4");
		Button btn5 = new Button("5");
		Button btn6 = new Button("6");
		Button btn7 = new Button("7");
		Button btn8 = new Button("8");
		Button btn9 = new Button("9");

		//演算子
		Button btnPlus = new Button("+");
		Button btnMinus = new Button("-");
		Button btnMultiplied = new Button("×");
		Button btnDivided = new Button("/");
		Button btnEqual = new Button("=");
		Button btnPlMi = new Button("±");
		Button btnPoint =  new Button(".");
		//ボタンのサイズ
		double a = 80;
		double b = 60;
		btn0.setPrefWidth(a);
		btn1.setPrefWidth(a);
		btn2.setPrefWidth(a);
		btn3.setPrefWidth(a);
		btn4.setPrefWidth(a);
		btn5.setPrefWidth(a);
		btn6.setPrefWidth(a);
		btn7.setPrefWidth(a);
		btn8.setPrefWidth(a);
		btn9.setPrefWidth(a);
		btnPlus.setPrefWidth(a);
		btnMinus.setPrefWidth(a);
		btnMultiplied.setPrefWidth(a);
		btnDivided.setPrefWidth(a);
		btnEqual.setPrefWidth(a);
		btnPlMi.setPrefWidth(a);
		btnPoint.setPrefWidth(a);

		btn0.setPrefHeight(b);
		btn1.setPrefHeight(b);
		btn2.setPrefHeight(b);
		btn3.setPrefHeight(b);
		btn4.setPrefHeight(b);
		btn5.setPrefHeight(b);
		btn6.setPrefHeight(b);
		btn7.setPrefHeight(b);
		btn8.setPrefHeight(b);
		btn9.setPrefHeight(b);
		btnPlus.setPrefHeight(b);
		btnMinus.setPrefHeight(b);
		btnMultiplied.setPrefHeight(b);
		btnDivided.setPrefHeight(b);
		btnEqual.setPrefHeight(b);
		btnPlMi.setPrefHeight(b);
		btnPoint.setPrefHeight(b);

		//ボタンの文字のサイズ
		int c = 20;
		btn0.setFont(new Font(c));
		btn1.setFont(new Font(c));
		btn2.setFont(new Font(c));
		btn3.setFont(new Font(c));
		btn4.setFont(new Font(c));
		btn5.setFont(new Font(c));
		btn6.setFont(new Font(c));
		btn7.setFont(new Font(c));
		btn8.setFont(new Font(c));
		btn9.setFont(new Font(c));
		btnPlus.setFont(new Font(c));
		btnMinus.setFont(new Font(c));
		btnMultiplied.setFont(new Font(c));
		btnDivided.setFont(new Font(c));
		btnEqual.setFont(new Font(c));
		btnPlMi.setFont(new Font(c));
		btnPoint.setFont(new Font(c));

		//ボタンの配置 行列で位置を表現
		GridPane grid = new GridPane();
		GridPane.setConstraints(btn7, 0, 0);
		GridPane.setConstraints(btn8, 1, 0);
		GridPane.setConstraints(btn9, 2, 0);
		GridPane.setConstraints(btn4, 0, 1);
		GridPane.setConstraints(btn5, 1, 1);
		GridPane.setConstraints(btn6, 2, 1);
		GridPane.setConstraints(btn1, 0, 2);
		GridPane.setConstraints(btn2, 1, 2);
		GridPane.setConstraints(btn3, 2, 2);
		GridPane.setConstraints(btn0, 1, 3);
		GridPane.setConstraints(btnPlus, 3, 0);
		GridPane.setConstraints(btnMinus, 3, 1);
		GridPane.setConstraints(btnMultiplied, 3, 2);
		GridPane.setConstraints(btnEqual, 3, 3);
		GridPane.setConstraints(btnPlMi, 0, 3);
		GridPane.setConstraints(btnPoint, 2, 3);

		grid.getChildren().addAll(btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9,
				btnPlus, btnMinus, btnMultiplied, btnEqual, btnPlMi, btnPoint);
		//ボタンとラベルを垂直に配置する
		VBox root = new VBox();
		root.getChildren().addAll(label, grid);

		stage.setScene(new Scene(root));
		stage.show();
	}


	public static void main(String[] args) {
		launch(args);
	}
}

次回は計算機能を実装していきたいと思います。