Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Code will help you 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

image text in transcribed

Code will help you

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("--"); } } } } }

Write a JavaFX program with the following controls: - Two RadioButton controls with the values Fahrenheit and Celsius. - TextField control that accepts temperature input from user. - Text that displays the converted tempture. - Button to convert the input tempatrure. The program should accept a temperature input form user, and the type of temperature conversion and display the calculated temperature result. Celsius Temperature Conversion Formula F=(C9/5)+32 Fahrenheit Temperature Conversion Formula C=(F32)5/9 Sample of expected output screen: Temperature Converter Fahrenheit From

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

More Books

Students also viewed these Databases questions

Question

Describe the seven standard parts of a letter.

Answered: 1 week ago

Question

Explain how to develop effective Internet-based messages.

Answered: 1 week ago

Question

Identify the advantages and disadvantages of written messages.

Answered: 1 week ago