HIDE NAV

Java FX Calculator 2026

It's a calculator!

CalcApp.java


package ceccs;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CalcApp extends Application {

    //TODO: add variable for entry state
    //TODO: add variables for entry values and operation
    //TODO: Buttons have to update the entries and display
    //TODO: Equals button actually CALCULATES!

    double appW = 400;
    double appH = 500;

    CalcDisplay display;
    CalcButtonPanel buttonPanel;

    @Override
    public void start(Stage primaryStage) throws Exception {
        display = new CalcDisplay(appW, appH/5);
        VBox rootVbox = new VBox(display);
        rootVbox.setSpacing(20);

        buttonPanel = new CalcButtonPanel(appH/5, this);
        rootVbox.getChildren().add(buttonPanel);

        buttonPanel.setAlignment(Pos.CENTER);

        Scene scn = new Scene(rootVbox);
        primaryStage.setScene(scn);
        primaryStage.setResizable(false);
        primaryStage.setTitle("CEC CS Calculator");
        primaryStage.show();
    }

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

CalcButton.java


package ceccs;

import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;

public class CalcButton extends StackPane {
    enum Type {
        NUMBER,
        OPERATION,
        EQUALS
    }

    public CalcApp app;
    private Rectangle bgRect;
    private Label label;
    private Type type;

    public CalcButton(String text, double size, Type t) {
        type = t;
        label = new Label(text);
        bgRect = new Rectangle(size, size, Color.DARKBLUE);
        getChildren().addAll(bgRect,label);
        label.setTextFill(Color.AQUA);
        label.setFont(new Font(size * .6));
        setOnMouseClicked(event -> {
            buttonAction();
        });
    }

    public void buttonAction(){
	//TODO: make buttons do something
   }

    public Type getType(){
        return type;
    }

    public String getText(){
        return label.getText();
    }

}

CalcButtonPannel.java


package ceccs;

import javafx.scene.layout.GridPane;

public class CalcButtonPanel extends GridPane {
    public CalcApp app;

    CalcButtonPanel(double buttonSize, CalcApp app){
        int butNum = 1;
        this.app = app;

        for(int j = 2; j >= 0; j--){
            for(int i = 0; i < 3; i++){
                CalcButton b = new CalcButton(String.valueOf(butNum), buttonSize, CalcButton.Type.NUMBER);
                setColumnIndex(b,i);
                setRowIndex(b,j);
                getChildren().add(b);
                butNum++;
                b.app = app;
            }
        }
        //plus and minus
        CalcButton plusButton = new CalcButton("+", buttonSize, CalcButton.Type.OPERATION);
        setColumnIndex(plusButton, 3);
        setRowIndex(plusButton, 0);
        getChildren().add(plusButton);
        plusButton.app = app;

        CalcButton minusButton = new CalcButton("-", buttonSize, CalcButton.Type.OPERATION);
        setColumnIndex(minusButton,3);
        setRowIndex(minusButton,1);
        getChildren().add(minusButton);
        minusButton.app = app;

        //equals
        CalcButton equalButton = new CalcButton("=", buttonSize, CalcButton.Type.EQUALS);
        setColumnIndex(equalButton, 3);
        setRowIndex(equalButton,2);
        getChildren().add(equalButton);
        equalButton.app = app;


        setHgap(10);
        setVgap(10);
    }
}

CalcDisplay.java


package ceccs;

import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;

public class CalcDisplay extends StackPane {

    private double width, height;
    private String text;
    private Label label;
    private Rectangle bgRect;

    public CalcDisplay(double w, double h){
        width = w;
        height = h;
        text = new String("");
        label = new Label(text);
        bgRect = new Rectangle(w,h, Color.DARKBLUE);
        getChildren().addAll(bgRect, label);
        label.setTextFill(Color.LIME);
        label.setFont(new Font(h*.75));
    }

    public void setText(String t){
        text = t;
        label.setText(t);
    }

    public String getText(){
        return text;
    }

}