2016-05-06 85 views
0

我正在創建這個小費計算器。出現是好的,但一旦我嘗試按計算提示沒有任何反應。我不確定我需要做的是提示或總計的功能。如果有人能給我一些見解,我將不勝感激。java - 小費計算器

下面是代碼:

import javafx.application.Application; 
import javafx.geometry.Pos; 
import javafx.scene.Scene; 
import javafx.scene.control.*; 
import javafx.collections.*; 
import javafx.scene.layout.*; 
import javafx.event.*; 
import javafx.stage.Stage; 

// Allows the user to enter a meal amount and select a tip rate. 
// When the claculate button is pressed the tip and total for the meal is displayed 

public class tips extends Application { 

    private TextField tfMeal = new TextField(); 
    private TextField tfTip = new TextField(); 
    private TextField tfTotal = new TextField(); 
    private ComboBox cbTips; 
    private ObservableList<String> tip_rates = 
      FXCollections.observableArrayList (
        "0.05", "0.10", 
        "0.15", "0.18", 
        "0.20", "0.22", 
        "0.25", "0.30"); 

    @Override 
    // Override the start method in the Application class 
    public void start(Stage primaryStage) { 

    VBox pane = new VBox(5);  

    tfMeal.setPrefColumnCount(10); 
    tfTip.setPrefColumnCount(5); 
    tfTotal.setPrefColumnCount(10); 

// Compmo box for tip rates 
    cbTips = new ComboBox(tip_rates); 
    cbTips.setVisibleRowCount(4); 
    cbTips.setValue(tip_rates.get(4)); 

    pane.getChildren().addAll(new Label("Amount: "), tfMeal, new Label("Tip rates: "), cbTips); 


    HBox hBox = new HBox(5); 
    Button btCalculate = new Button("Calculate Tip"); 

    hBox.setAlignment(Pos.CENTER); 
    hBox.getChildren().addAll(btCalculate, new Label("Tip: "), tfTip, new Label("Total: "), tfTotal); 
    // tip and total are display only fields 
    tfTip.setEditable(false); 
    tfTotal.setEditable(false); 

    BorderPane borderPane = new BorderPane(); 
    borderPane.setCenter(pane); 
    borderPane.setBottom(hBox); 
    BorderPane.setAlignment(hBox, Pos.TOP_CENTER); 

    // Create a scene and place it in the stage 
    Scene scene = new Scene(borderPane, 375, 150); 
    primaryStage.setTitle("Tip calculator"); // Set the stage title 
    primaryStage.setScene(scene); // Place the scene in the stage 
    primaryStage.show(); // Display the stage 

    btCalculate.setOnAction(new EventHandler<ActionEvent>() { 

     public void handle(ActionEvent e) { 
      // take the values in tfMeal and cbTip 
      // calculate and display in tfTip and tfTotal as the tip and total 
      // for extra credit add exception handling (but do not display the error in the console) 

      // this statement gets the tip rate 
      double tiprt = Double.parseDouble(cbTips.getValue().toString()); 

     } 
    }); 



    } 

    /** 
    * The main method is only needed for the IDE with limited 
    * JavaFX support. Not needed for running from the command line. 
    */ 
    public static void main(String[] args) { 
    launch(args); 
    } 
} 

enter image description here

回答

0

預期你的程序運行;你需要在這裏添加你的功能。下面是實現代碼:

btCalculate.setOnAction(new EventHandler<ActionEvent>() { 

     public void handle(ActionEvent e) { 
      // take the values in tfMeal and cbTip 
      // calculate and display in tfTip and tfTotal as the tip and total 
      // for extra credit add exception handling (but do not display the error in the console) 

      // this statement gets the tip rate 
      double tiprt = Double.parseDouble(cbTips.getValue().toString()); 
      double tfMealVal = Double.parseDouble(tfMeal.getText().toString()); 
      double rslt = tfMealVal*tiprt; 
      double totalAmnt = rslt+tfMealVal; 
      tfTip.setText(Double.toString(rslt)); 
      tfTotal.setText(Double.toString(totalAmnt)); 

     } 
    }); 

輸出:

enter image description here

+0

5%?你怪物... –

+0

嘿,我是印度人。根據我們的標準,5%是很重要的:P – PseudoAj

+0

在香港,10%是標準,肯定可以給更多 – PSo

0

你不是實際上是試圖做任何事情的處理程序有用呢。所有你需要的是一個基本的數學計算。

要獲得小費金額,您必須將金額乘以小費率。

tipAmount = amount * tipRate; 

要獲得總金額,您必須將小費金額添加到原始金額。

total = amount + tipAmount; 

然後,您需要設置相應的文本字段的值與那些新計算的金額。