Advertisement
Guest User

Untitled

a guest
May 5th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.geometry.Pos;
  3. import javafx.scene.Scene;
  4. import javafx.scene.control.*;
  5. import javafx.collections.*;
  6. import javafx.scene.layout.*;
  7. import javafx.event.*;
  8. import javafx.stage.Stage;
  9.  
  10. // Allows the user to enter a meal amount and select a tip rate.
  11. // When the claculate button is pressed the tip and total for the meal is displayed
  12.  
  13. public class tips extends Application {
  14.  
  15. private TextField tfMeal = new TextField();
  16. private TextField tfTip = new TextField();
  17. private TextField tfTotal = new TextField();
  18. private ComboBox cbTips;
  19. private ObservableList<String> tip_rates =
  20. FXCollections.observableArrayList (
  21. "0.05", "0.10",
  22. "0.15", "0.18",
  23. "0.20", "0.22",
  24. "0.25", "0.30");
  25.  
  26. @Override
  27. // Override the start method in the Application class
  28. public void start(Stage primaryStage) {
  29.  
  30. VBox pane = new VBox(5);
  31.  
  32. tfMeal.setPrefColumnCount(10);
  33. tfTip.setPrefColumnCount(5);
  34. tfTotal.setPrefColumnCount(10);
  35.  
  36. // Compmo box for tip rates
  37. cbTips = new ComboBox(tip_rates);
  38. cbTips.setVisibleRowCount(4);
  39. cbTips.setValue(tip_rates.get(4));
  40.  
  41. pane.getChildren().addAll(new Label("Amount: "), tfMeal, new Label("Tip rates: "), cbTips);
  42.  
  43.  
  44. HBox hBox = new HBox(5);
  45. Button btCalculate = new Button("Calculate Tip");
  46.  
  47. hBox.setAlignment(Pos.CENTER);
  48. hBox.getChildren().addAll(btCalculate, new Label("Tip: "), tfTip, new Label("Total: "), tfTotal);
  49. // tip and total are display only fields
  50. tfTip.setEditable(false);
  51. tfTotal.setEditable(false);
  52.  
  53. BorderPane borderPane = new BorderPane();
  54. borderPane.setCenter(pane);
  55. borderPane.setBottom(hBox);
  56. BorderPane.setAlignment(hBox, Pos.TOP_CENTER);
  57.  
  58. // Create a scene and place it in the stage
  59. Scene scene = new Scene(borderPane, 375, 150);
  60. primaryStage.setTitle("Tip calculator"); // Set the stage title
  61. primaryStage.setScene(scene); // Place the scene in the stage
  62. primaryStage.show(); // Display the stage
  63.  
  64. btCalculate.setOnAction(new EventHandler<ActionEvent>() {
  65.  
  66. public void handle(ActionEvent e) {
  67. // take the values in tfMeal and cbTip
  68. // calculate and display in tfTip and tfTotal as the tip and total
  69. // for extra credit add exception handling (but do not display the error in the console)
  70.  
  71. // this statement gets the tip rate
  72. double tiprt = Double.parseDouble(cbTips.getValue().toString());
  73.  
  74. }
  75. });
  76.  
  77.  
  78.  
  79. }
  80.  
  81. /**
  82. * The main method is only needed for the IDE with limited
  83. * JavaFX support. Not needed for running from the command line.
  84. */
  85. public static void main(String[] args) {
  86. launch(args);
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement