Question

use this code of converting Km to miles , to create Temperature converter by using java...

use this code of converting Km to miles , to create Temperature converter by using java FX

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;

/**
* 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<ActionEvent>
{
@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));
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;

/**
* Temperature Converter application
*/

public class TemperatureConverter extends Application {
   // Fields
   private TextField celsiusTextField;
   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 temperature in celsius degrees:");

       // Create a TextField for input.
       celsiusTextField = 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 celsiusTextField in an HBox.
       HBox hbox = new HBox(10, promptLabel, celsiusTextField);

       // 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("Temperature Converter");

       // Show the window.
       primaryStage.show();
   }

   /*
   * Event handler class for calcButton
   */

   class CalcButtonHandler implements EventHandler<ActionEvent> {
       @Override
       public void handle(ActionEvent event) {
           // Get the temperature.
           Double celsius = Double.parseDouble(celsiusTextField.getText());

           // Convert the Celsius to Fahrenheit
           Double fahrenheit = (celsius*9/5) + 32;

           // Display the results.
           resultLabel.setText(String.format("%,.2f F", fahrenheit));
       }
   }
}

OUTPUT-

Temperature Converter - 0 x Enter a temperature in celsius degrees: 12 Convert 53.60 F

Add a comment
Know the answer?
Add Answer to:
use this code of converting Km to miles , to create Temperature converter by using java...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use Kilometer Converter application code to write Temperature Converter application to convert degrees Fahrenheit into degrees...

    Use Kilometer Converter application code to write Temperature Converter application to convert degrees Fahrenheit into degrees Celsius ((F - 32)*5/9). It needs to be JavaFX 1 import javafx.application. Application; 2 import javafx.stage. Stage; 3 import javafx.scene. Scene; 4 import javafx.scene.layout.HBox; 5 import javafx.scene.layout. VBox; 6 import javafx.geometry.Pos; 7 import javafx.geometry.Insets; 8 import javafx.scene.control.Label; 9 import javafx.scene.control. TextField; 10 import javafx.scene.control.Button; 11 import javafx.event. EventHandler; 12 import javafx.event. ActionEvent; 13 14 ** 15 * Kilometer Converter application 16 17 18 public...

  • (5 points) Analyze the following codes. b) import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import...

    (5 points) Analyze the following codes. b) import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class Test extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { Button btOK = new Button("OK"); Button btCancel = new Button("Cancel"); EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { System.out.println("The OK button is clicked"); } }; btOK.setOnAction(handler); btCancel.setOnAction(handler); HBox pane = new HBox(5); pane.getChildren().addAll(btOK, btCancel); Scene...

  • Examine the following code and complete missing parts: import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets;...

    Examine the following code and complete missing parts: import javafx.application.Application; import javafx.event.ActionEvent; 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; public class NtoThePowerOfN extends Application{ @Override public void start(Stage primaryStage) throws Exception { TextField inputField; TextField outputField; VBox base = new VBox(10); base.setPadding(new Insets(10)); base.setAlignment(Pos.CENTER); // // A: input components - label and text field // // // B: button - to compute the value // // //...

  • This is my code. I need the UML for this code My code: public class Main...

    This is my code. I need the UML for this code My code: public class Main extends FlightManager {    Stage window;    Scene scene;    Button button;    HBox layout = new HBox(20);    Label dateTxt, airlineTxt, originTxt, destTxt, clssTxt, scopeTxt, adultsTxt, childTxt;    @Override public void start(Stage stage) { // create the UI and show it here    window = stage; window.setTitle("Reservations 2020"); button = new Button("New Flight");    VBox group3 = new VBox(10); dateTxt = Text("", group3);...

  • # Java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class Test extends Application {...

    # Java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class Test extends Application {   @Override // Override the start method in the Application class   public void start(Stage primaryStage) {     // Create a button and place it in the scene     Button btOK = new Button("OK");     btOK.setOnAction(e -> System.out.println("OK 1"));     btOK.setOnAction(e -> System.out.println("OK 2"));     Scene scene = new Scene(btOK, 200, 250);     primaryStage.setTitle("MyJavaFX"); // Set the stage title     primaryStage.setScene(scene); // Place the scene in the stage     primaryStage.show(); // Display the stage...

  • Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “...

    Java: In this assignment, you will create an accumulator accumulator-calculator that displays a sad face “ :-( “ whenever the number displayed by the calculator is negative and a happy face “ :-) ” whenever the number displayed is positive. The calculator responds to the following commands: num + , num - , num * , num / and C ( Clear ). After initial run, if the user clicks 8 and then presses the “+” button for example, the...

  • / Finish the code to make it work import static javafx.application.Application.launch; import java.io.File; import javafx.application.Application; import...

    / Finish the code to make it work import static javafx.application.Application.launch; import java.io.File; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.media.AudioClip; import javafx.stage.Stage; public class JukeBox extends Application { private ChoiceBox<String> choice; private AudioClip[] tunes; private AudioClip current; private Button playButton, stopButton;    //----------------------- // presents an interface that allows the user to select and play // a tune from a drop down box //-----------------------   ...

  • A game clock that starts at zero and begins to count up in seconds. Add buttons...

    A game clock that starts at zero and begins to count up in seconds. Add buttons to pause and resume the clock. Given this initial code: import javafx.animation.KeyFrame; import javafx.animation.Timeline; 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.stage.Stage; import javafx.util.Duration; import javafx.scene.paint.Color; public class AnimationCounter2 extends Application{ @Override public void start(Stage stage) throws Exception{ // Create the label and align its contents. Label label = new Label("0"); label.setTextFill(Color.RED); label.setStyle("-fx-font-size: 4em;"); label.setAlignment(Pos.CENTER); EventHandler<ActionEvent> handler =...

  • JavaFX! Just need to fill several lanes of code please!!! CSE205 OOP and Data Structure Quiz #15 Last Name (print) First Name (print) Write a JavaFX GUI application program that simulates a timer. T...

    JavaFX! Just need to fill several lanes of code please!!! CSE205 OOP and Data Structure Quiz #15 Last Name (print) First Name (print) Write a JavaFX GUI application program that simulates a timer. The timer should show "count: the beginning and increase the number by 1 in every one second, and so on. o" at .3 A Timer - × A Timer Count: 0 Count: 1 (B) After 1 second, the GUI Window (A) Initial GUI Window According to the...

  • The JavaFX framework provides more than one way to handle events. For event handlers, we could...

    The JavaFX framework provides more than one way to handle events. For event handlers, we could use inner classes, anonymous inner classes, or the new Java 8 feature of lambda expressions. In this discussion, you will explore these different ways of writing event handles in JavaFX. To prepare for this discussion, you must unzip the attached NetBeans project zip file (U4D1_HandleEvents.zip) and load it into your NetBeans IDE. The project uses an inner class to handle the click event on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT