I need to create buttons for addition, subtraction, and multiplication functiona
ID: 3772524 • Letter: I
Question
I need to create buttons for addition, subtraction, and multiplication functionality using the appropriate event handler import javafx.application.Application; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.event.*; import javafx.stage.Stage; public class test33 extends Application { private double num1 = 0, num2 = 0, result = 0; @Override // Override the start method in the Application class public void start(Stage primaryStage) { FlowPane pane = new FlowPane(); pane.setHgap(2); TextField tfNumber1 = new TextField(); TextField tfNumber2 = new TextField(); TextField tfResult = new TextField(); tfNumber1.setPrefColumnCount(3); tfNumber2.setPrefColumnCount(3); tfResult.setPrefColumnCount(3); pane.getChildren().addAll(new Label("Number 1: "), tfNumber1, new Label("Number 2: "), tfNumber2, new Label("Result: "), tfResult); // Create four buttons HBox hBox = new HBox(5); Button btAdd = new Button("Add"); hBox.setAlignment(Pos.CENTER); hBox.getChildren().addAll(btAdd); BorderPane borderPane = new BorderPane(); borderPane.setCenter(pane); borderPane.setBottom(hBox); BorderPane.setAlignment(hBox, Pos.TOP_CENTER); // Create a scene and place it in the stage Scene scene = new Scene(borderPane, 375, 150); primaryStage.setTitle("Test33"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage btAdd.setOnAction(new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { num1 = Double.parseDouble(tfNumber1.getText()); num2 = Double.parseDouble(tfNumber2.getText()); result = num1 + num2; tfResult.setText(String.format("%.1f", result)); } }); } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }
Explanation / Answer
import javafx.application.Application ;
import javafx.stage.Stage ;
import javafx.scene.Scene ;
import javafx.scene.control.Button ;
import javafx.scene.control.Label ;
import javafx.scene.control.Menu ;
import javafx.scene.control.MenuBar ;
import javafx.scene.control.MenuItem ;
import javafx.scene.control.TextField ;
import javafx.scene.input.KeyCombination ;
import javafx.scene.layout.HBox ;
import javafx.scene.layout.VBox ;
import javafx.geometry.Pos ;
public class MenuDemo extends Application
{
private TextField tfNumber1 = new TextField ();
private TextField tfNumber2 = new TextField ();
private TextField tfResult = new TextField();
public void start(Stage primaryStage)
{
MenuBar menuBar = new MenuBar();
Menu menuOperation = new Menu("Operation");
Menu menuExit = new Menu("Exit");
menuBar.getMenus().addAll(menuOperation, menuExit);
MenuItem menuItemAdd = new MenuItem("Add");
MenuItem menuItemSubtract = new MenuItem("Subtract");
MenuItem menuItemMultiply = new MenuItem("Multiply");
MenuItem menuItemDivide = new MenuItem("Divide");
menuOperation.getItems().addAll(menuItemAdd, menuItemSubtract,menuItemMultiply, menuItemDivide);
MenuItem menuItemClose = new MenuItem("Close");
menuExit.getItems().add(menuItemClose);
menuItemAdd.setAccelerator(KeyCombination.keyCombination("Ctrl+A"));
menuItemSubtract.setAccelerator(KeyCombination.keyCombination("Ctrl+S"));
menuItemMultiply.setAccelerator(KeyCombination.keyCombination("Ctrl+M"));
menuItemDivide.setAccelerator(KeyCombination.keyCombination("Ctrl+D"));
HBox hBox1 = new HBox(5);
tfNumber1.setPrefColumnCount(2);
tfNumber2.setPrefColumnCount(2);
tfResult.setPrefColumnCount(2);
hBox1.getChildren().addAll(new Label("Number 1:"), tfNumber1,
new Label("Number 2:"), tfNumber2, new Label("Result:"),tfResult);
hBox1.setAlignment(Pos.CENTER);
HBox hBox2 = new HBox(5);
Button btAdd = new Button("Add");
Button btSubtract = new Button("Subtract");
Button btMultiply = new Button("Multiply");
Button btDivide = new Button("Divide");
hBox2.getChildren().addAll(btAdd, btSubtract, btMultiply, btDivide);
hBox2.setAlignment(Pos.CENTER);
VBox vBox = new VBox(10);
vBox.getChildren().addAll(menuBar, hBox1, hBox2);
Scene scene = new Scene(vBox, 300, 250);
primaryStage.setTitle("MenuDemo"); // Set the window title
primaryStage.setScene(scene); // Place the scene in the window
primaryStage.show(); // Display the window
menuItemAdd.setOnAction(e > perform('+'));
menuItemSubtract.setOnAction(e - > perform(' - '));
menuItemMultiply.setOnAction(e - > perform('*'));
menuItemDivide.setOnAction(e - > perform('/'));
menuItemClose.setOnAction(e - > System.exit(0));
// Handle button actions
btAdd.setOnAction(e - > perform('+'));
btSubtract.setOnAction(e - > perform(' - '));
btMultiply.setOnAction(e - > perform('*'));
btDivide.setOnAction(e - > perform('/'));
}
private void perform(char operator) {
double number1 = Double.parseDouble(tfNumber1.getText());
double number2 = Double.parseDouble(tfNumber2.getText());
double result = 0;
switch (operator)
{
case '+': result = number1 + number2; break;
case '-': result = number1 - number2; break;
case '*': result = number1 * number2; break;
case '/': result = number1 / number2; break;
}
tfResult.setText(result + "");
};
public static void main(String[] args)
{
launch(args);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.