Question
I need help with this assignment, I have to write a user interface for your calculator using JavaFX. For this assignment, the GUI does (not)
I need help with this assignment,
I have to write a user interface for your calculator using JavaFX. 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.
The code below makes up an unorganized GUI, The buttons should stick to each other in 4 row and 4 columns, where the fourth column have the signs(/,*,-,+).
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
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
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
root.getChildren().addAll(expressionLabel, numberGrid, operatorGrid);
primaryStage.setScene(scene); primaryStage.sizeToScene(); primaryStage.show(); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started