0% found this document useful (0 votes)
54 views7 pages

JavaFX Calculator Application Code

This document contains the code for a JavaFX calculator application. It defines a Calc class that extends Application and overrides the start method to launch the calculator UI. The UI consists of a TextField for the display and a TilePane of buttons. Buttons are created from a template and handle numeric input or operations. The value is tracked and operations are applied when equals is pressed, updating the displayed value.

Uploaded by

Ayano Boresa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views7 pages

JavaFX Calculator Application Code

This document contains the code for a JavaFX calculator application. It defines a Calc class that extends Application and overrides the start method to launch the calculator UI. The UI consists of a TextField for the display and a TilePane of buttons. Buttons are created from a template and handle numeric input or operations. The value is tracked and operations are applied when equals is pressed, updating the displayed value.

Uploaded by

Ayano Boresa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
  • JavaFX Calculator Application

package application;

import [Link];

import [Link];

import [Link].*;

import [Link];

import [Link];

import [Link].*;

import [Link];

import [Link].*;

import [Link].*;

import [Link].*;

public class Calc extends Application {

private static final String[][] template = {

{"7", "8", "9", "/"},

{"4", "5", "6", "*"},

{"1", "2", "3", "-"},

{"0", "c", "=", "+"}

};

private final Map<String, Button> accelerators = new HashMap<>();

private final DoubleProperty stackValue = new SimpleDoubleProperty();

private final DoubleProperty value = new SimpleDoubleProperty();


private enum Op {NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE}

private Op curOp = [Link];

private Op stackOp = [Link];

public static void main(String[] args) {

launch(args);

@Override

public void start(Stage stage) {

final TextField screen = createScreen();

final TilePane buttons = createButtons();

[Link]("Calculator");

[Link]([Link]);

[Link](false);

[Link](new Scene(createLayout(screen, buttons)));

[Link]();

private VBox createLayout(TextField screen, TilePane buttons) {

final VBox layout = new VBox(20);

[Link]([Link]);
[Link]("-fx-background-color: chocolate; -fx-padding: 20; -fx-font-size: 20;");

[Link]().setAll(screen, buttons);

handleAccelerators(layout);

[Link]().bind([Link]());

return layout;

private void handleAccelerators(VBox layout) {

[Link](KeyEvent.KEY_PRESSED, keyEvent -> {

Button activated = [Link]([Link]());

if (activated != null) {

[Link]();

});

private TextField createScreen() {

final TextField screen = new TextField();

[Link]("-fx-background-color: aquamarine;");

[Link](Pos.CENTER_RIGHT);

[Link](false);

[Link]().bind([Link]("%.0f", value));

return screen;

}
private TilePane createButtons() {

TilePane buttons = new TilePane();

[Link](7);

[Link](7);

[Link](template[0].length);

for (String[] r : template) {

for (String s : r) {

[Link]().add(createButton(s));

return buttons;

private Button createButton(final String s) {

Button button = makeStandardButton(s);

if ([Link]("[0-9]")) {

makeNumericButton(s, button);

} else {

final ObjectProperty<Op> triggerOp = determineOperand(s);

if ([Link]() != [Link]) {

makeOperandButton(button, triggerOp);

} else if ("c".equals(s)) {

makeClearButton(button);

} else if ("=".equals(s)) {
makeEqualsButton(button);

return button;

private ObjectProperty<Op> determineOperand(String s) {

final ObjectProperty<Op> triggerOp = new SimpleObjectProperty<>([Link]);

switch (s) {

case "+" -> [Link]([Link]);

case "-" -> [Link]([Link]);

case "*" -> [Link]([Link]);

case "/" -> [Link]([Link]);

return triggerOp;

private void makeOperandButton(Button button, final ObjectProperty<Op> triggerOp) {

[Link]("-fx-base: lightgray;");

[Link](actionEvent -> curOp = [Link]());

private Button makeStandardButton(String s) {

Button button = new Button(s);


[Link]("-fx-base: beige;");

[Link](s, button);

[Link](Double.MAX_VALUE, Double.MAX_VALUE);

return button;

private void makeNumericButton(final String s, Button button) {

[Link](actionEvent -> {

if (curOp == [Link]) {

[Link]([Link]() * 10 + [Link](s));

} else {

[Link]([Link]());

[Link]([Link](s));

stackOp = curOp;

curOp = [Link];

});

private void makeClearButton(Button button) {

[Link]("-fx-base: mistyrose;");

[Link](actionEvent -> [Link](0));

private void makeEqualsButton(Button button) {


[Link]("-fx-base: ghostwhite;");

[Link](actionEvent -> {

switch (stackOp) {

case ADD -> [Link]([Link]() + [Link]());

case SUBTRACT -> [Link]([Link]() - [Link]());

case MULTIPLY -> [Link]([Link]() * [Link]());

case DIVIDE -> [Link]([Link]() / [Link]());

});

package application;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.propert
private enum Op {NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE}
    private Op curOp = Op.NOOP;
    private Op stackOp = Op.NOOP;
layout.setStyle("-fx-background-color: chocolate; -fx-padding: 20; -fx-font-size: 20;");
        layout.getChildren()
private TilePane createButtons() {
        TilePane buttons = new TilePane();
        buttons.setVgap(7);
        buttons
makeEqualsButton(button);
            }
        }
        return button;
    }
    private ObjectProperty<Op>
button.setStyle("-fx-base: beige;");
        accelerators.put(s, button);
        button.setMaxSize(Double.MAX_VALUE,
button.setStyle("-fx-base: ghostwhite;");
        button.setOnAction(actionEvent -> {
            switch (stackOp) {

You might also like