JavaFX User Interface for Calculator In this assignment, you will write a user i
ID: 3708477 • Letter: J
Question
JavaFX User Interface for Calculator
In this assignment, you will write a user interface for your calculator using JavaFX. Your graphical user interface (GUI) should look like the screenshot below (creativity is good, but you also need to be able to design to customer specifications). When the user closes the window, the program should end. For this assignment the GUI does not need to respond to any other user input. Note that we are developing this GUI completely separately from any class. This is a common design pattern that is often called ModelView-Controller, or MVC. The model is the data your program deals with, the view is the user interface, and the controller responds to input from the view by acting on the data in the model. Developing programs in this way allows you to create different user interfaces (e.g. desktop, web-based, mobile phone) without modifying the rest of your program.
You will be graded according to the following rubric (each item is worth one point):
? A window is displayed in the center of the screen when the program begins. The window has the title Calculator
? Closing the window ends the program Copyright © 2017, 2018 Sinclair Community College. All Rights Reserved.
? There is a text field at the top of the window with an initial value of 0.0; this value cannot be changed (call the setEditable(false) method on the instance of the TextField class)
? There are keys for all of the digits and operators in the center of the window
? There is an equals button stretched along the entire bottom of the window
? The layout of the components within the window is the same as in the screenshot above
? The text shown in the text field is bold and in 20 point mono-spaced font
? Your program compiles
? Your program runs
? You follow standard coding conventions (e.g. variable names, indentation, comments, etc.)
? Note: If your program does not compile, you will receive a score of 0 on the entire assignment
? Note: If you program compiles but does not run, you will receive a score of 0 on the entire assignment
? Note: I your Eclipse project is not exported correctly, you will receive a score of 0 on the entire assignment
? Note: If your Eclipse project is not exported and uploaded to the eLearn drop box correctly, you will receive a score of 0 on the entire assignment
? Note: If you do not submit code that solves the problem for this particular assignment, you will not receive any points for the program’s compiling, the program’s running, or following standard coding conventions
? Note: This assignment must be done using JavaFX. If it is not done using JavaFX, you will receive a score of 0 on the entire assignment
Sample output: Replace Copy Paraguph ? ????Explanation / Answer
Program:
package javafxapplication2;
import java.awt.Color;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Calculator extends Application{
private boolean operatorAlreadyPressed = false;
private boolean secondOperand = false;
private boolean scriptExceptionOccurred = false;
public static void main(String[] args){
Application.launch(args);
}
public void start(Stage primaryStage) throws ScriptException{
int windowWidth = 190;
int windowHeight = 250;
primaryStage.setTitle("Calculator");
primaryStage.setWidth(windowWidth);
primaryStage.setHeight(windowHeight);
VBox root = new VBox();
Scene scene = new Scene(root, windowWidth, windowHeight, javafx.scene.paint.Color.WHITE);
Label expressionLabel = new Label();
GridPane numberGrid = new GridPane();
numberGrid.setHgap(5);
numberGrid.setVgap(5);
GridPane operatorGrid = new GridPane();
operatorGrid.setHgap(5);
operatorGrid.setVgap(5); ArrayList<Button> numberButtons = new ArrayList<>();
ArrayList<Button> operatorButtons = new ArrayList<>();
int buttonCounter = 0;
int buttonWidth = 500;
int buttonHeight = 500;
for(int y = 0; y < 2; y++){
RowConstraints row = new RowConstraints();
row.setPercentHeight(20);
numberGrid.getRowConstraints().add(row);
for(int x = 0; x < 5 && buttonCounter < 10; x++, buttonCounter++){
if(y == 0){
ColumnConstraints column = new ColumnConstraints();
column.setPercentWidth(33);
column.setHalignment(HPos.CENTER);
numberGrid.getColumnConstraints().add(column);
}
Button button = new Button(String.valueOf(buttonCounter));
button.setPrefWidth(buttonWidth);
button.setPrefHeight(buttonHeight);
numberButtons.add(button);
numberGrid.add(button, x, y);
}
}
String[][] operatorTextArr = {{"+", "-"}, {"*", "/"}, {"=", "c"}};
for(int y = 0; y < operatorTextArr.length; y++){
RowConstraints row = new RowConstraints();
row.setPercentHeight(20);
operatorGrid.getRowConstraints().add(row);
for(int x = 0; x < operatorTextArr[y].length; x++){
if(y == 0){
ColumnConstraints column = new ColumnConstraints();
column.setPercentWidth(33);
column.setHalignment(HPos.CENTER);
operatorGrid.getColumnConstraints().add(column);
}
Button button = new Button(operatorTextArr[y][x]);
button.setPrefWidth(buttonWidth);
button.setPrefHeight(buttonHeight);
operatorButtons.add(button);
operatorGrid.add(button, y, x);
}
}
for(int counter = 0; counter < numberButtons.size(); counter++){
numberButtons.get(counter).setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent e){
Button temp = (Button)e.getSource();
if(scriptExceptionOccurred){
expressionLabel.setText("");
scriptExceptionOccurred = false;
}
String newexpressionText = expressionLabel.getText() + temp.getText();
expressionLabel.setText(newexpressionText);
System.out.println(newexpressionText);
if(operatorAlreadyPressed && !secondOperand){
secondOperand = true;
}
}
}
}
for(int counter = 0; counter < operatorButtons.size(); counter++){
operatorButtons.get(counter).setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent e){
String text = expressionLabel.getText();
Button temp = (Button)e.getSource();
if(scriptExceptionOccurred){
expressionLabel.setText("");
scriptExceptionOccurred = false;
}
if(temp.getText().equals("c") && expressionLabel.getText().length() > 0){
char charToDelete = text.charAt(text.length() - 1);
if(charToDelete == '+' || charToDelete == '-'
|| charToDelete == '*' || charToDelete == '/'){
operatorAlreadyPressed = false;
}
expressionLabel.setText(expressionLabel.getText().substring(
0, expressionLabel.getText().length()-1));
}
else if(!operatorAlreadyPressed && text.length() > 0&& !temp.getText().equals("=")){
String newexpressionText = expressionLabel.getText()
+ temp.getText();
expressionLabel.setText(newexpressionText);
operatorAlreadyPressed = true;
}
else if(secondOperand || temp.getText().equals("=")){
operatorAlreadyPressed = false;
secondOperand = false;
try{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
expressionLabel.setText(String.valueOf(engine.eval(
expressionLabel.getText())));
if(!temp.getText().equals("=") && !temp.getText().equals("c")){
expressionLabel.setText(expressionLabel.getText()
+ temp.getText());
operatorAlreadyPressed = true;
}
}
catch(ScriptException exc){
expressionLabel.setText("Invalid operation.");
scriptExceptionOccurred = true;
secondOperand = false;
operatorAlreadyPressed = false;
}
}
}
}
}
root.getChildren().addAll(expressionLabel, numberGrid, operatorGrid);
primaryStage.setScene(scene);
primaryStage.sizeToScene();
primaryStage.show();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.