Question
Needs help with Javafx programming This is a Temperature convention program I just want to add this point to my code When the Convert Button
Needs help with Javafx programming
This is a Temperature convention program
I just want to add this point to my code
When the Convert Button in the center of the window is pressed any potential exceptions that might be thrown should be caught and their toString() printed out to the output text field.
Hint: You can check that this works by not worrying about the format of the input
and making it throw exceptions by putting in non-numbers
----
Here is my code
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 600, 600, Color.WHITE);
// this code is for the label for entering the number
Label lbl = new Label("Enter a temperature:");
lbl.setLayoutX(100);
lbl.setLayoutY(110);
root.getChildren().add(lbl);
TextField txt = new TextField();
txt.setLayoutX(300);
txt.setLayoutY(110);
root.getChildren().add(txt);
Label lbl1 = new Label("Input Scale");
lbl1.setLayoutX(120);
lbl1.setLayoutY(160);
root.getChildren().add(lbl1);
// this is for the first toggle group
ToggleGroup group = new ToggleGroup();
// this is for the second toggle group
ToggleGroup group2 = new ToggleGroup();
// this code is for RadioButton
RadioButton f = new RadioButton("Celcius");
f.setLayoutX(100);
f.setLayoutY(200);
group.getToggles().add(f);
root.getChildren().add(f);
RadioButton a = new RadioButton("Fahrenheite");
a.setLayoutX(100);
a.setLayoutY(230);
group.getToggles().add(a);
root.getChildren().add(a);
RadioButton r = new RadioButton("Kelvin");
r.setLayoutX(100);
r.setLayoutY(260);
group.getToggles().add(r);
root.getChildren().add(r);
RadioButton e = new RadioButton("Celcius");
e.setLayoutX(400);
e.setLayoutY(200);
group2.getToggles().add(e);
root.getChildren().add(e);
Label lbl2 = new Label("Output Scale");
lbl2.setLayoutX(420);
lbl2.setLayoutY(160);
root.getChildren().add(lbl2);
RadioButton q = new RadioButton("Fahrenheite");
q.setLayoutX(400);
q.setLayoutY(230);
group2.getToggles().add(q);
root.getChildren().add(q);
RadioButton w = new RadioButton("Kelvin");
w.setLayoutX(400);
w.setLayoutY(260);
group2.getToggles().add(w);
root.getChildren().add(w);
Label lbl3 = new Label("Converted temperature:");
lbl3.setLayoutX(100);
lbl3.setLayoutY(310);
root.getChildren().add(lbl3);
// the outputs
TextField tt = new TextField();
tt.setLayoutX(300);
tt.setLayoutY(310);
root.getChildren().add(tt);
// these are strings to convert
//
Button con = new Button("Convert");
con.setLayoutX(250);
con.setLayoutY(170);
// this is an event handler to print out
// the inputscale and the outputscale
con.setOnAction(event -> {
System.out.print(((RadioButton) group.getSelectedToggle()).getText() + " ");
System.out.println(((RadioButton) group2.getSelectedToggle()).getText());
String a2 = txt.getText();
//All the above if statements are to convert
//the input to output depending on the RadioButton and user choose
if(((RadioButton) group.getSelectedToggle()).getText().equals("Celcius")
&& ((RadioButton) group2.getSelectedToggle()).getText().equals("Celcius")) {
System.out.println("1");
double a4 = Double.parseDouble(a2);
double a5 = a4 ;
String total = Double.toString(a5);
tt.setText(total);
}
if(((RadioButton) group.getSelectedToggle()).getText().equals("Celcius")
&& ((RadioButton) group2.getSelectedToggle()).getText().equals("Fahrenheite")) {
System.out.println("2");
double a4 = Double.parseDouble(a2);
double a5 = (a4 * 9/5) + 32 ;
String total = Double.toString(a5);
tt.setText(total);
}
if(((RadioButton) group.getSelectedToggle()).getText().equals("Celcius")
&& ((RadioButton) group2.getSelectedToggle()).getText().equals("Kelvin")) {
double a4 = Double.parseDouble(a2);
double a5 = a4 + 273.15 ;
String total = Double.toString(a5);
tt.setText(total);
}
if(((RadioButton) group.getSelectedToggle()).getText().equals("Fahrenheite")
&& ((RadioButton) group2.getSelectedToggle()).getText().equals("Celcius")) {
double a4 = Double.parseDouble(a2);
double a5 = (a4 - 32) * 5/9 ;
String total = Double.toString(a5);
tt.setText(total);
}
if(((RadioButton) group.getSelectedToggle()).getText().equals("Fahrenheite")
&& ((RadioButton) group2.getSelectedToggle()).getText().equals("Fahrenheite")) {
double a4 = Double.parseDouble(a2);
double a5 = a4 ;
String total = Double.toString(a5);
tt.setText(total);
}
if(((RadioButton) group.getSelectedToggle()).getText().equals("Fahrenheite")
&& ((RadioButton) group2.getSelectedToggle()).getText().equals("Kelvin")) {
double a4 = Double.parseDouble(a2);
double a5 = (a4 - 32) * 5/9 + 273.15;
String total = Double.toString(a5);
tt.setText(total);
}
if(((RadioButton) group.getSelectedToggle()).getText().equals("Kelvin")
&& ((RadioButton) group2.getSelectedToggle()).getText().equals("Celcius")) {
double a4 = Double.parseDouble(a2);
double a5 = a4 - 273.15 ;
String total = Double.toString(a5);
tt.setText(total);
}
if(((RadioButton) group.getSelectedToggle()).getText().equals("Kelvin")
&& ((RadioButton) group2.getSelectedToggle()).getText().equals("Fahrenheite")) {
double a4 = Double.parseDouble(a2);
double a5 = (a4 - 273.15) * 9/5 + 32 ;
String total = Double.toString(a5);
tt.setText(total);
}
if(((RadioButton) group.getSelectedToggle()).getText().equals("Kelvin")
&& ((RadioButton) group2.getSelectedToggle()).getText().equals("Kelvin")) {
double a4 = Double.parseDouble(a2);
double a5 = a4 ;
String total = Double.toString(a5);
tt.setText(total);
}
});
root.getChildren().add(con);
// title
primaryStage.setTitle("Temperature Conversion");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
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