Answer to Question #162033 in Java | JSP | JSF for hajma

Question #162033

Write a temperature conversion GUI that converts from farenheit to celsuis


1
Expert's answer
2021-02-13T05:03:50-0500
import javafx.application.Application;
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.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;


public class GUIConverter extends Application {


    @Override
    public void start(Stage primaryStage) throws Exception {
        Label tempLabel = new Label("Temperature in Celsius: ");
        TextField farenheitField = new TextField();
        farenheitField.setMaxWidth(150);
        Button convertButton = new Button("Convert");
        convertButton.setOnMouseClicked((MouseEvent me) -> {
            if (me.getButton() == MouseButton.PRIMARY) {
                try {
                    tempLabel.setText(String.format("Temperature in Celsius: %.2f",
                            (Double.parseDouble(farenheitField.getText()) - 32) * 5 / 9));
                } catch (NumberFormatException e) {
                }
            }
        });
        VBox vBox = new VBox(10, tempLabel, farenheitField, convertButton);
        vBox.setAlignment(Pos.CENTER);
        primaryStage.setScene(new Scene(new StackPane(vBox)));
        primaryStage.setWidth(250);
        primaryStage.setHeight(300);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog