this is my main class. package application; import javafx.application.Applicatio
ID: 3809954 • Letter: T
Question
this is my main class.
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
// define all GUI controls as data fields
Button btnAdd, btnSubtract, btnMultiply, btnDivide, btnExponent, btnLoad;
Label lbl1, lbl2, lbl3;
TextField txfValue1, txfValue2;
TextArea txa;
@Override
public void start(Stage primaryStage) {
Pane grdRootPane = buildGuiPane();
Scene scene = new Scene(grdRootPane,475, 275);
primaryStage.setScene(scene);
primaryStage.setTitle("MyLittleCalculator");
primaryStage.show();
}
private Pane buildGuiPane() {
btnAdd = new Button("add");
btnSubtract = new Button("subtract");
btnMultiply= new Button("multiply");
btnDivide = new Button("divide");
btnLoad = new Button("load");
btnExponent = new Button("exp");
lbl1 = new Label("Operand 1: ");
lbl2 = new Label("Operand 2: ");
lbl3 = new Label("Result: ");
txfValue1 = new TextField();
txfValue2 = new TextField();
txa = new TextArea();
txa.setEditable(false);
txa.setPrefColumnCount(30);
txa.setPrefRowCount(5);
//We use gridPane1 to implement the first row of the calculator
GridPane gridPane1 = new GridPane();
gridPane1.setAlignment(Pos.CENTER);
gridPane1.setHgap(200);
gridPane1.setVgap(5);
gridPane1.add(lbl1, 0, 0);
gridPane1.add(lbl2, 0, 1);
gridPane1.add(txfValue1, 1, 0);
gridPane1.add(txfValue2, 1, 1);
//We use gridPane2 to implement the second row of the calculator
GridPane gridPane2 = new GridPane();
gridPane2.setAlignment(Pos.CENTER);
gridPane2.setHgap(20);
// write your code here to add GUI controls to gridPane2. See the code about gridPane1 for hint
gridPane2.add(btnAdd, 1, 2);
gridPane2.add(btnSubtract, 2, 2);
gridPane2.add(btnMultiply, 3, 2);
gridPane2.add(btnDivide, 4, 2);
gridPane2.add(btnLoad, 5, 2);
gridPane2.add(btnExponent, 6, 2);
//We use gridPane3 to implement the last row of the calculator
GridPane gridPane3 = new GridPane();
gridPane3.setAlignment(Pos.CENTER);
gridPane3.setHgap(10);
//write your code here to add GUI controls to gridPane3
gridPane3.add(lbl3, 0, 0);
gridPane3.add(txa, 1, 0);
// We create our top-level pane here.
GridPane topPane = new GridPane();
topPane.setAlignment(Pos.CENTER);
topPane.setVgap(20);
//Here we add gridPane1, gridPane2 and gridPane3 to our topPane, which is our final layout
topPane.add(gridPane1, 0, 0);
topPane.add(gridPane2, 0, 1);
topPane.add(gridPane3, 0, 2);
// Write you code here to register all the buttons to the same event handler function named "calculate". We will implement this function later.
btnAdd.setOnAction(e -> { calculate(e);});
btnSubtract.setOnAction(e -> { calculate(e);});
btnMultiply.setOnAction(e -> { calculate(e);});
btnDivide.setOnAction(e -> { calculate(e);});
btnLoad.setOnAction(e -> { calculate(e);});
btnExponent.setOnAction(e -> { calculate(e);});
return topPane;
}
public void calculate(ActionEvent e){
// write your code to implement this button-clicking event handler
}
public static void main(String[] args) {
launch(args);
}
}
i was made class. but i don't know how can i make
public void calculate(ActionEvent e){
// write your code to implement this button-clicking event handler
}
this part.
plesae help me.
i need to make like this.
My culator 30.0 Operand 1: operand 2: add multiply subtract divide load 30.0 Result The meanings of the buttons for add subtract", "multiply" divide are straightforward When button "load" is pressed, the value in "Result" textArea will be loaded as operand 1" for subsequent calculations. For example, when "load" is pressed, the value "30.0" will be loaded to the textField of "Operand 1". When button "exp" is pressed, you should raise "Operand 1" to the power of operand 2" For example, if "Operand 1" is 4 and "Operand 2" is 3, the result should be 4 4 4 64Explanation / Answer
public void calculate(ActionEvent e){
// write your code to implement this button-clicking event handler
int a= Integer.parseInt(txfValue1.getText());
int b= Integer.parseInt(txfValue2.getText());
if(e.getSource()==btnAdd){
int c = a+b;
txa.setText(String.valueOf(c));
}
else if(e.getSource()==btnSubtract){
int c = a-b;
txa.setText(String.valueOf(c));
}
else if(e.getSource()==btnMultiply){
int c = a*b;
txa.setText(String.valueOf(c));
}
else if(e.getSource()==btnDivide){
int c = a/b;
txa.setText(String.valueOf(c));
}
else if(e.getSource()==btnLoad){
String l=txa.getText();
txa.setText(l);
}
else if(e.getSource()==btnExponent){
double p = Math.pow(a, b);
txa.setText(String.valueOf(p));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.