Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the following code must be changed to implement the scenebuilder where the buttons would be linked to your code so a fxml file would be

the following code must be changed to implement the scenebuilder where the buttons would be linked to your code so a fxml file would be present, the code should also be commented

import javafx.application.Application;

import javafx.beans.binding.Bindings;

import javafx.beans.property.DoubleProperty;

import javafx.beans.property.ObjectProperty;

import javafx.beans.property.SimpleDoubleProperty;

import javafx.beans.property.SimpleObjectProperty;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.geometry.Pos;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.input.KeyEvent;

import javafx.scene.layout.TilePane;

import javafx.scene.layout.VBox;

import javafx.stage.Stage;

import javafx.stage.StageStyle;

import java.util.HashMap;

import java.util.Map;

public class Calculator extends Application {

private enum AirthmaticOperation {

ADD, SUBTRACT, MULTIPLY, DIVIDE,NONE

}

private AirthmaticOperation currentOperation = AirthmaticOperation.NONE;

private AirthmaticOperation previousOperation = AirthmaticOperation.NONE;

private DoubleProperty stackValue = new SimpleDoubleProperty();

private DoubleProperty value = new SimpleDoubleProperty();

private final Map buttonValue = new HashMap<>();

private static final String[][] calculatorArray =

{ { "1", "2", "3", "+" },

{ "4", "5", "6", "-" },

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

{ "0", "c", "=", "*" }

};

public static void main(String[] args) {

launch(args);

}

@Override

public void start(Stage stage) {

TextField screen = new TextField();

screen.setStyle("-fx-background-color: white;");

screen.setAlignment(Pos.CENTER_RIGHT);

screen.setEditable(false);

screen.textProperty().bind(Bindings.format("%.0f", value));

TilePane buttons = new TilePane();

buttons.setVgap(7);

buttons.setHgap(7);

buttons.setPrefColumns(calculatorArray[0].length);

for (String[] r : calculatorArray) {

for (String s : r) {

buttons.getChildren().add(createButton(s));

}

}

VBox layout = new VBox(20);

layout.setAlignment(Pos.CENTER);

layout.setStyle("-fx-background-color: black; -fx-padding: 50; -fx-font-size: 50;");

layout.getChildren().setAll(screen, buttons);

layout.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler() {

@Override

public void handle(KeyEvent keyEvent) {

Button activated = buttonValue.get(keyEvent.getText());

if (activated != null) {

activated.fire();

}

}

});

screen.prefWidthProperty().bind(buttons.widthProperty());

stage.setTitle("Calculator");

stage.initStyle(StageStyle.UTILITY);

stage.setResizable(false);

stage.setScene(new Scene(layout));

stage.show();

}

private Button createButton(final String s) {

Button button = new Button(s);

button.setStyle("-fx-base: white;");

buttonValue.put(s, button);

button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);

if (s.matches("[0-9]")) {

button.setOnAction(new EventHandler() {

@Override

public void handle(ActionEvent actionEvent) {

if (currentOperation == AirthmaticOperation.NONE) {

value.set(value.get() * 10 + Integer.parseInt(s));

} else {

stackValue.set(value.get());

value.set(Integer.parseInt(s));

previousOperation = currentOperation;

currentOperation = AirthmaticOperation.NONE;

}

}

});

} else {

ObjectProperty triggerOp = new SimpleObjectProperty<>(AirthmaticOperation.NONE);

switch (s) {

case "+":

triggerOp.set(AirthmaticOperation.ADD);

break;

case "-":

triggerOp.set(AirthmaticOperation.SUBTRACT);

break;

case "*":

triggerOp.set(AirthmaticOperation.MULTIPLY);

break;

case "/":

triggerOp.set(AirthmaticOperation.DIVIDE);

break;

}

if (triggerOp.get() != AirthmaticOperation.NONE) {

button.setStyle("-fx-base: gray;");

button.setOnAction(new EventHandler() {

@Override

public void handle(ActionEvent actionEvent) {

currentOperation = triggerOp.get();

}

});

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

button.setStyle("-fx-base: red;");

button.setOnAction(new EventHandler() {

@Override

public void handle(ActionEvent actionEvent) {

value.set(0);

}

});

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

button.setStyle("-fx-base: gray;");

button.setOnAction(new EventHandler() {

@Override

public void handle(ActionEvent actionEvent) {

switch (previousOperation) {

case ADD:

value.set(stackValue.get() + value.get());

break;

case SUBTRACT:

value.set(stackValue.get() - value.get());

break;

case MULTIPLY:

value.set(stackValue.get() * value.get());

break;

case DIVIDE:

value.set(stackValue.get() / value.get());

break;

}

}

});

}

}

return button;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Joe Celkos Data And Databases Concepts In Practice

Authors: Joe Celko

1st Edition

1558604324, 978-1558604322

More Books

Students also viewed these Databases questions

Question

Describe the rationale behind short-selling.

Answered: 1 week ago

Question

Explain what is meant by the terms unitarism and pluralism.

Answered: 1 week ago