Question
In java modify the program to allow the user to do 2 types of conversions: Kilometers to miles or Meters to yards. import java.awt.event.ActionEvent; import
In java modify the program to allow the user to do 2 types of conversions: Kilometers to miles or Meters to yards.
import java.awt.event.ActionEvent;
import javafx.application.Application; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage;
/** * Kilometer Converter application */
public class KiloConverter extends Application { // Fields private TextField kiloTextField; private Label resultLabel; public static void main(String[] args) { // Launch the application. launch(args); } @Override public void start(Stage primaryStage) { // Create a Label to display a prompt. Label promptLabel = new Label("Enter a distance in kilometers:"); // Create a TextField for input. kiloTextField = new TextField(); // Create a Button to perform the conversion. Button calcButton = new Button("Convert"); // Register the event handler. calcButton.setOnAction(new CalcButtonHandler()); // Create an empty Label to display the result. resultLabel = new Label(); // Put the promptLabel and the kiloTextField in an HBox. HBox hbox = new HBox(10, promptLabel, kiloTextField); // Put the HBox, calcButton, and resultLabel in a VBox. VBox vbox = new VBox(10, hbox, calcButton, resultLabel); // Set the VBox's alignment to center. vbox.setAlignment(Pos.CENTER); // Set the VBox's padding to 10 pixels. vbox.setPadding(new Insets(10)); // Create a Scene. Scene scene = new Scene(vbox);
// Add the Scene to the Stage. primaryStage.setScene(scene); // Set the stage title. primaryStage.setTitle("Kilometer Converter"); // Show the window. primaryStage.show(); } /* * Event handler class for calcButton */ class CalcButtonHandler implements EventHandler { @Override public void handle(ActionEvent event) { // Get the kilometers. Double kilometers = Double.parseDouble(kiloTextField.getText()); // Convert the kilometers to miles. Double miles = kilometers * 0.6214; // Display the results. resultLabel.setText(String.format("%,.2f miles", miles)); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started