Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package com.example.javafxlecture02; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.RadioButton; import javafx.scene.control.TextField; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import

package com.example.javafxlecture02;

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.RadioButton; import javafx.scene.control.TextField; import javafx.scene.control.ToggleGroup; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.stage.Stage;

import java.io.IOException;

public class simpleCalculator extends Application {

//Declare controls as instance variables //Because we need to use these controls in simpleCalculator class and calculatorHandler class //Declare and instantiate three text fields TextField input1 = new TextField(); TextField input2 = new TextField(); TextField calcResult = new TextField();

//Declare and instantiate two radio buttons RadioButton add; RadioButton sub;

//main method with a call to the launch() method that will call the start(Stage stage) javaFX method public static void main(String[] args) { launch(); }

@Override public void start(Stage stage) throws IOException {

//create the root node (GridPane, VBox, HBox, etc..) GridPane root = new GridPane(); //set the vertical gap between controls to 20 root.setVgap(20); //set the root alignment to center root.setAlignment(Pos.CENTER);

//create 4 labels Label num1 = new Label("Number 1: "); Label num2 = new Label("Number 2: "); Label choose = new Label("Calculation method: "); Label result = new Label("Result: ");

//add Labels to the root root.add(num1, 0, 0); root.add(num2, 0, 1); root.add(choose, 0, 2); root.add(result, 0, 3);

//add the three TextFileds to the root root.add(input1, 1, 0); root.add(input2, 1, 1); root.add(calcResult, 1, 3);

//add the two radio button add = new RadioButton("+"); sub = new RadioButton("-"); //create a toggle group for the radio buttons ToggleGroup tg = new ToggleGroup(); //add the radio buttons to the toggle group add.setToggleGroup(tg); sub.setToggleGroup(tg);

//create an HBox HBox hbox = new HBox(); //set spacing between controls in Hbox to 20 hbox.setSpacing(20); //add the radio buttons to hbox hbox.getChildren().addAll(add, sub); //set hbox alignment to center hbox.setAlignment(Pos.CENTER); //add the hbox to the root root.add(hbox, 1, 2);

//call the setOnAction on both radio button , when clicked will call the handle method add.setOnAction(new calculatorHandler()); sub.setOnAction(new calculatorHandler());

//create a scene //set the root in the scene Scene scene = new Scene(root, 300, 200); //set the scene in the stage stage.setScene(scene); stage.setTitle("Simple Calculator");

//show the stage stage.show();

}

//create event handler class that implements the EventHandler interface class calculatorHandler implements EventHandler {

//void method handle with parameter of the event class or it's subclasses public void handle(ActionEvent actionEvent) { //check which radio button was chosen by calling the getSource method of the actionEvent object //if add radio button was selected if (actionEvent.getSource() == add) { //check if both number text fields are not empty if (!(input1.getText().isEmpty()) && !(input2.getText().isEmpty())) { //parse the values entered in textfields to integer ( parse from string to integer) int result = Integer.parseInt(input1.getText()) + Integer.parseInt(input2.getText()); //set the result text field to the result (convert from int to string) calcResult.setText(String.valueOf(result)); } else { //if both text fields empty, display this text calcResult.setText("--"); } //if sub radio button was selected } else if (actionEvent.getSource() == sub) { //check if both number text fields are not empty if (!(input1.getText().isEmpty()) && !(input2.getText().isEmpty())) { int result = Integer.parseInt(input1.getText()) - Integer.parseInt(input2.getText()); calcResult.setText(String.valueOf(result)); } else { calcResult.setText("--"); } } } } }

image text in transcribed

Use the code below to solve the problem

Use the Lab "simpleCalculator" code and modify it as following: - Create four buttons controls instead of RadioButton control to perform the below calculations: 1. Add two numbers (+) 2. Subtract two numbers () 3. Multiply two numbers () 4. Divide two numbers (/) Note: Make sure to handle division by zero Feel free to modify the code to meet your desired outcome. Samnle nutnut

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Infrastructure For Medical Research In Databases

Authors: Thomas Heinis ,Anastasia Ailamaki

1st Edition

1680833480, 978-1680833485

More Books

Students also viewed these Databases questions

Question

What is the purpose of a digital audit trail?

Answered: 1 week ago

Question

What are the specific things I will do to improve my sensitivity?

Answered: 1 week ago

Question

in a defined benefit plan, a formula is used that

Answered: 1 week ago