HIDE NAV

Java FX Calculator 2026

It's a calculator!

CalcApp.java


...

public class CalcApp extends Application {
    ...

    // ADD these member vars to address previous TODOs
    int entryStep = 1;
    // on Entry step 1 we only accept numbers move to step 1 if a number is entered
    // on Entry step 2 we will accept numbers or operation. move to step 3 when an op. enetered
    // on entry step 3 we accept numbers. once a single number is entered move to step 4
    // on entry step 4 accept numbers or equals.
    double firstEntry, secondEntry, result;
    String secondEntryString;
    String operation;

    ...

    @Override
    public void start(Stage primaryStage) throws Exception {...}

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

}

CalcButton.java


...

public class CalcButton extends StackPane {
    enum Type {...}

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

    public CalcButton(String text, double size, Type t) { ... }

    //TODO: make app display result on screen (only system print currently)
    //TODO: loop back for entry (clear screen?), but make sure the result is displayed
    //TODO: Add a backspace key to remove entries. It may need a new key type
    //TODO: add operations * and / possibly exponent ^
    //TODO: add decimal point .  Only allow one per entry and it must follow a number
    //TODO: negative number entry. Should be distinct from minus operaiton. May need new type.

    public void buttonAction(){
	switch(app.entryStep) {
            case 1:
                if(type == Type.NUMBER) {
                    addToScreen();
                    app.entryStep++;
                }
                break;
            case 2:
                if(type == Type.NUMBER) addToScreen();
                else if (type == Type.OPERATION) {
                    double value = Double.valueOf(app.display.getText());
                    app.firstEntry = value;
                    System.out.println("the first value is " + app.firstEntry);
                    addToScreen();
                    app.operation = getText();
                    System.out.println("the operation is: " + app.operation);
                    app.entryStep++;
                }
                break;
            case 3:
                if(type == Type.NUMBER) {
                    app.secondEntryString = new String();
                    app.secondEntryString += label.getText();
                    addToScreen();
                    app.entryStep++;
                }
                break;
            case 4:
                if(type == Type.NUMBER){
                    addToScreen();
                    app.secondEntryString += getText();
                } else if(type == Type.EQUALS) {
                    double value = Double.valueOf(app.secondEntryString);
                    app.secondEntry = value;
                    addToScreen();
                    System.out.println("the second value is " + app.secondEntry);
                    //System.out.println("NEED TO COMPUTE");
                    //Compute the value

                    switch(app.operation){
                        case "-":
                            app.result = app.firstEntry - app.secondEntry;
                            break;
                        case "+":
                            app.result = app.firstEntry + app.secondEntry;
                            break;

                    }
                    System.out.println("result = " + app.result);
                    //TODO: display this on screen
                }
                break;
        }    
    }

    ...

}

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);
        //TODO: we need a color and size!
        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;
    }

}