In: Computer Science
Hello, I am using IntelliJ IDEA with JavaFX to build a travel expensive calculator, but I dont konw how to conver user input to doulbe. Pleasse teach me how to add all textfield and display it after I click submit.
public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Label label1 = new Label ("(1) Number of days on the trip"); Label label2 = new Label ("(2) Transportation cost (choose one only)"); Label label3 = new Label ("Airfare Cost "); Label label4 = new Label ("Miles driven "); Label label5 = new Label ("(3) Conference registration cost "); Label label6 = new Label ("(4) Lodging Cost (per night) "); Label label7 = new Label ("(5) Food cost (total) "); Label label8 = new Label ("(6) Lodging Cost (per night) "); Label TotalExpensive = new Label("Total expenses: "); Label TotalExpensiveResult = new Label(" "); TextField field1 = new TextField(); TextField field2 = new TextField(); TextField field3 = new TextField(); TextField field4 = new TextField(); TextField field5 = new TextField(); TextField field6 = new TextField(); TextField field7 = new TextField(); Button button1 = new Button ("Submit"); //Create submit button Button button2 = new Button ("Cancel"); //Create Cancel button HBox hBox = new HBox(20, label1, field1); HBox hBox1 = new HBox(10, label2); HBox hBox2 = new HBox(15, label3, field2); HBox hBox3 = new HBox(10, label4, field3); HBox hBox4 = new HBox(12, label5, field4); HBox hBox5 = new HBox(32, label6, field5); HBox hBox6 = new HBox(75, label7, field6); HBox hBox7 = new HBox(32, label8, field7); HBox hBox8 = new HBox(20, button1, button2); HBox hBox9 = new HBox(20, TotalExpensive, TotalExpensiveResult); hBox.setAlignment(Pos.BASELINE_RIGHT); hBox1.setAlignment(Pos.BASELINE_LEFT); hBox2.setAlignment(Pos.BASELINE_RIGHT); hBox3.setAlignment(Pos.BASELINE_RIGHT); hBox4.setAlignment(Pos.BASELINE_RIGHT); hBox5.setAlignment(Pos.BASELINE_RIGHT); hBox6.setAlignment(Pos.BASELINE_RIGHT); hBox7.setAlignment(Pos.BASELINE_RIGHT); hBox8.setAlignment(Pos.CENTER); hBox9.setAlignment(Pos.CENTER); hBox.setPadding(new Insets(0,0,15,0)); hBox3.setPadding(new Insets(0,0,15,0)); hBox8.setPadding(new Insets(25,0,15,0)); GridPane gridPane = new GridPane (); gridPane.add(hBox, 0, 0); gridPane.add(hBox1, 0, 1); gridPane.add(hBox2, 0, 2); gridPane.add(hBox3, 0, 3); gridPane.add(hBox4, 0, 4); gridPane.add(hBox5, 0, 5); gridPane.add(hBox6, 0, 6); gridPane.add(hBox7, 0, 7); gridPane.add(hBox8, 0, 8); gridPane.add(hBox9, 0, 9); //*******Here********* button1.setOnAction(event -> { double cost = field1; TotalExpensiveResult.setText("Total Cost: $" + cost); }); gridPane.setAlignment(Pos.CENTER); gridPane.setPadding( new Insets(20, 20, 20, 20)); gridPane.setVgap( 10); gridPane.setHgap( 10); primaryStage.setTitle("Travel Expenses Calculator"); primaryStage.setScene(new Scene (gridPane)); primaryStage.show(); }
CODE
import javafx.application.Application;
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.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Label label1 = new Label ("(1) Number of days on the trip");
Label label2 = new Label ("(2) Transportation cost (choose one only)");
Label label3 = new Label ("Airfare Cost ");
Label label4 = new Label ("Miles driven ");
Label label5 = new Label ("(3) Conference registration cost ");
Label label6 = new Label ("(4) Lodging Cost (per night) ");
Label label7 = new Label ("(5) Food cost (total) ");
Label label8 = new Label ("(6) Lodging Cost (per night) ");
Label TotalExpensive = new Label("Total expenses: ");
Label TotalExpensiveResult = new Label(" ");
TextField field1 = new TextField();
TextField field2 = new TextField();
TextField field3 = new TextField();
TextField field4 = new TextField();
TextField field5 = new TextField();
TextField field6 = new TextField();
TextField field7 = new TextField();
Button button1 = new Button ("Submit"); //Create submit button
Button button2 = new Button ("Cancel"); //Create Cancel button
HBox hBox = new HBox(20, label1, field1);
HBox hBox1 = new HBox(10, label2);
HBox hBox2 = new HBox(15, label3, field2);
HBox hBox3 = new HBox(10, label4, field3);
HBox hBox4 = new HBox(12, label5, field4);
HBox hBox5 = new HBox(32, label6, field5);
HBox hBox6 = new HBox(75, label7, field6);
HBox hBox7 = new HBox(32, label8, field7);
HBox hBox8 = new HBox(20, button1, button2);
HBox hBox9 = new HBox(20, TotalExpensive, TotalExpensiveResult);
hBox.setAlignment(Pos.BASELINE_RIGHT);
hBox1.setAlignment(Pos.BASELINE_LEFT);
hBox2.setAlignment(Pos.BASELINE_RIGHT);
hBox3.setAlignment(Pos.BASELINE_RIGHT);
hBox4.setAlignment(Pos.BASELINE_RIGHT);
hBox5.setAlignment(Pos.BASELINE_RIGHT);
hBox6.setAlignment(Pos.BASELINE_RIGHT);
hBox7.setAlignment(Pos.BASELINE_RIGHT);
hBox8.setAlignment(Pos.CENTER);
hBox9.setAlignment(Pos.CENTER);
hBox.setPadding(new Insets(0,0,15,0));
hBox3.setPadding(new Insets(0,0,15,0));
hBox8.setPadding(new Insets(25,0,15,0));
GridPane gridPane = new GridPane ();
gridPane.add(hBox, 0, 0);
gridPane.add(hBox1, 0, 1);
gridPane.add(hBox2, 0, 2);
gridPane.add(hBox3, 0, 3);
gridPane.add(hBox4, 0, 4);
gridPane.add(hBox5, 0, 5);
gridPane.add(hBox6, 0, 6);
gridPane.add(hBox7, 0, 7);
gridPane.add(hBox8, 0, 8);
gridPane.add(hBox9, 0, 9);
//*******Here*********
button1.setOnAction(event -> {
double cost = Double.parseDouble(field1.getText());
TotalExpensiveResult.setText("Total Cost: $" + cost);
});
gridPane.setAlignment(Pos.CENTER);
gridPane.setPadding( new Insets(20, 20, 20, 20));
gridPane.setVgap( 10);
gridPane.setHgap( 10);
primaryStage.setTitle("Travel Expenses Calculator");
primaryStage.setScene(new Scene(gridPane));
primaryStage.show();
}
}