Question: Please help me with this java assignment using javafx. To do this, you will need to create an instance of MemoryCalc in the CalcGUI and
Please help me with this java assignment using javafx.
To do this, you will need to create an instance of MemoryCalc in the CalcGUI and write action listeners to pass information back and forth between the calculator and the GUI. There are many possible ways to achieve this, but the easiest is probably to develop four different action listeners: DigitListener This action listener is added to the 0-9 and . (dot) keys. If the equals button was just pressed, this action listener will overwrite the total shown in the text field with the value of the key (its label) that was just pressed. If the equals button was not just pressed, this action listener will append the current key's label onto the end of the string shown in the text field. OperatorListener This action listener is added to the +, -, *, and / keys. It sets the value of a class field to the operator that the user has chosen. Clear Listener This action listener is added to the C key. It calls the calculator's clear method and sets the value of the text field back to 0.0. EqualsListener This action listener is added to the = key. It reads the current value from the text field, converts it to a double, and calls the appropriate calculator method based on the current operator, passing in the double value. The calculator will compute the answer, and then this action listener will update the value in the text field to the calculator's current total.When the EqualsListener is converting the text shown in the text field into a double value in order to pass it to the calculator, be careful to handle the case where the text field contains an invalid value, such as 6..72. In this case you should catch the exception, display an error message to the user, and abort the call to the calculator (just restore the current total to the text field).
This what I got so far but it doesn't work propertly, please someone help me to get this done.
package myJavaFx;
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Labeled; import javafx.scene.control.TextField; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.stage.Stage;
public class javaFx extends Application { private MemoryCalculator calc; private TextField txtDisplay; MemoryCalculator clc = new MemoryCalculator(); public void start(Stage primaryStage){ calc = new MemoryCalculator(); GridPane buttonPane = new GridPane(); buttonPane.setHgap(0); buttonPane.setVgap(0); for (int i = 0; i < 4; i++) { ColumnConstraints column = new ColumnConstraints(); column.setPercentWidth(25); buttonPane.getColumnConstraints().add(column); } Button bt7 = new Button("7"); bt7.setPrefSize(60, 60); bt7.setOnAction(digitListener); Button bt8 = new Button("8"); bt8.setPrefSize(60, 60); bt8.setOnAction(digitListener); Button bt9 = new Button("9"); bt9.setPrefSize(60, 60); bt9.setOnAction(digitListener);
Button btD = new Button("/"); btD.setPrefSize(60, 60);
buttonPane.addRow(1, bt7, bt8, bt9, btD); Button bt4 = new Button("4"); bt4.setPrefSize(60, 60); bt4.setOnAction(digitListener); Button bt5 = new Button("5"); bt5.setPrefSize(60, 60); bt5.setOnAction(digitListener); Button bt6 = new Button("6"); bt6.setPrefSize(60, 60); bt6.setOnAction(digitListener);
Button btM = new Button("*"); btM.setPrefSize(60, 60); buttonPane.addRow(2, bt4, bt5, bt6, btM); Button bt1 = new Button("1"); bt1.setPrefSize(60, 60); bt1.setOnAction(digitListener); Button bt2 = new Button("2"); bt2.setPrefSize(60, 60); bt2.setOnAction(digitListener); Button bt3 = new Button("3"); bt3.setPrefSize(60, 60); bt3.setOnAction(digitListener);
Button btS = new Button("-"); btS.setPrefSize(60, 60); buttonPane.addRow(3, bt1, bt2, bt3, btS); Button btC = new Button("C"); btC.setPrefSize(60, 60); btC.setOnAction(clearListener); Button bt0 = new Button("0"); bt0.setPrefSize(60, 60); bt0.setOnAction(digitListener); Button btDot = new Button("."); btDot.setPrefSize(60, 60); btDot.setOnAction(digitListener);
Button btP = new Button("+"); btP.setPrefSize(60, 60); btP.setOnAction(operatorListener); buttonPane.addRow(4, btC, bt0, btDot, btP); Button btE = new Button("="); btE.setPrefSize(240, 60); btE.setOnAction(equalsListener); buttonPane.add(btE, 0, 5, 4, 1); txtDisplay = new TextField(); txtDisplay.setEditable(false); txtDisplay.setText("0.0"); buttonPane.add(txtDisplay, 0, 0, 4, 1); Scene scene = new Scene(buttonPane); primaryStage.setTitle("GUI Calc"); primaryStage.setScene(scene); primaryStage.sizeToScene(); primaryStage.show(); } public static void main(String[] args) { launch(args); } private EventHandler digitListener = new EventHandler() { public void handle (ActionEvent actionEvent) { String button = ((Labeled) actionEvent.getSource()).getText(); if(txtDisplay.getText().equals("0.0")) txtDisplay.setText(button); else { txtDisplay.setText(txtDisplay.getText() + button); } } }; private EventHandler operatorListener = new EventHandler(){
@Override public void handle(ActionEvent e) { Button btn = (Button) e.getSource(); String str = btn.getText();
if (str.equals("+")) { double num2 = Double.parseDouble(txtDisplay.getText()); clc.add(num2); txtDisplay.setText(String.valueOf(clc.getCurrentValue())); } else if (str.equals("-")) { double num2 = Double.parseDouble(txtDisplay.getText()); clc.subtract(num2); txtDisplay.setText(String.valueOf(clc.getCurrentValue())); } else if (str.equals("*")){ double num2 = Double.parseDouble(txtDisplay.getText()); clc.multiply(num2); txtDisplay.setText(String.valueOf(clc.getCurrentValue())); } else if (str.equals("/")){ double num2 = Double.parseDouble(txtDisplay.getText()); clc.divide(num2); txtDisplay.setText(String.valueOf(clc.getCurrentValue())); }
System.out.println(btn); System.out.println(str); } }; private EventHandler clearListener = new EventHandler(){ @Override public void handle(ActionEvent e) { Button btn = (Button) e.getSource(); String str = btn.getText(); if (str.equals("C")) { clc.clear(); txtDisplay.setText(String.valueOf(clc.getCurrentValue())); } } }; private EventHandler equalsListener = new EventHandler(){ @Override public void handle(ActionEvent e) { Button btn = (Button) e.getSource(); String str = btn.getText(); if (str.equals("=")) { } } };
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
