我和我的搭档在单击按钮的ActionEvent
时遇到问题。它不计算单个项目的价格。
说明:
包括
Button
的事件处理程序方法,用于计算 “总帐单”如下:ObservableList
中的“价格”字段 与ListView
中所选的“项目”相匹配(用户可以 一次仅购买一件商品)乘以营业税(8.625%)加上 提示,它是ObservableList
中匹配的“价格”乘以Slider
中的百分比;在“结果”中显示文字标签Label
所选的“项目”,其匹配的“价格”,营业税, “小费”和“总帐单”的金额
代码:
public class TipCalculator extends Application
{
private DecimalFormat twoDecimals;
//private float percentage;
private Label lblHeader;
private Label lblResults;
private Label lblTip;
private ObservableList<String> item;
private ObservableList<Double> price;
private Slider sliderTipPercent;
private FlowPane flowTipPercent;
private ListView listView;
private VBox vBoxSliders;
private VBox vBoxListView;
private VBox vBoxResults;
private Button btnCalculate;
//NumberFormat currency = NumberFormat.getCurrencyInstance();
@Override
public void start(Stage primaryStage)
{
// Instantiates a DecimalFormat object to format the Labels linked to the Sliders
twoDecimals = new DecimalFormat("0.00");
// Make the heading Label and format
lblHeader = new Label("McDonald's Order");
lblHeader.setMinWidth(800);
lblHeader.setAlignment(Pos.TOP_CENTER);
lblHeader.setStyle("-fx-font-family: 'Impact'; -fx-font-size: 28px; -fx-text-fill: yellow; -fx-background-color: rgb(212, 31, 6); -fx-font-weight: bold;");
// Instantiate 10 Orders
Order order1 = new Order("Big Mac", 9.75);
Order order2 = new Order("Chicken Nuggets", 3.25);
Order order3 = new Order("Quarter Pounder", 8.25);
Order order4 = new Order("Double Quarter Pounder", 9.75);
Order order5 = new Order("Filet O'Fish", 8.25);
Order order6 = new Order("Bacon Clubhouse", 9.75);
Order order7 = new Order("McGriddle", 4.25);
Order order8 = new Order("McDouble", 2.25);
Order order9 = new Order("McChicken", 2.25);
Order order10 = new Order("Cheeseburger", 2.25);
// Instantiate and format the "hours worked" Slider and the elements linked to it
// The "hours worked" Slider and Label are placed side by side in a FlowPane
lblTip = new Label("Tip Percent");
sliderTipPercent = new Slider(0.0, 25.0, 15.0);
sliderTipPercent.setShowTickMarks(true);
sliderTipPercent.setShowTickLabels(true);
sliderTipPercent.setMajorTickUnit(5.0);
sliderTipPercent.setMinorTickCount(4);
sliderTipPercent.setPrefWidth(300);
sliderTipPercent.setOnMouseDragged( e -> updateTipPercentSlider(e) );
lblTip = new Label( twoDecimals.format( sliderTipPercent.getValue() ) );
flowTipPercent = new FlowPane(sliderTipPercent, lblTip);
flowTipPercent.setHgap(10);
// Place the Sliders and related elements into the "sliders" VBox
vBoxSliders = new VBox(7, flowTipPercent);
vBoxSliders.setMinWidth(500);
vBoxSliders.setPadding( new Insets(10) );
// Instantiate the "results" Label and "calculate" Button
// Place those elements into the "results" VBox and format
lblResults = new Label("Result:");
lblResults.setStyle("-fx-font-family: 'Arial'; -fx-font-size: 25px; -fx-font-weight: bold; -fx-text-fill: black;");
btnCalculate = new Button("Calculate Total Bill");
btnCalculate.setPadding( new Insets(10) );
btnCalculate.setMaxWidth(150);
btnCalculate.setStyle("-fx-font-family: 'Impact'; -fx-background-color: rgb(212, 31, 6); -fx-font-size: 15px; -fx-font-weight: bold; -fx-text-fill: yellow;");
btnCalculate.setOnAction( e -> handleClickAction(e) );
vBoxResults = new VBox(35, lblTip, btnCalculate, lblResults);
btnCalculate.setAlignment(Pos.BOTTOM_CENTER);
vBoxResults.setPadding( new Insets(15) );
vBoxResults.setPrefWidth(175);
vBoxResults.setPrefHeight(100);
vBoxResults.setStyle("-fx-background-color: white; -fx-border-color: black; -fx-border-radius: 20;");
// Instantiate on ObservableList of titles from the Book class
// Instantiate an ObservableList of Book objects
item = FXCollections.observableArrayList (order1.getItem(), order2.getItem(),
order3.getItem(), order4.getItem(), order5.getItem(), order6.getItem(),
order7.getItem(), order8.getItem(), order9.getItem(), order10.getItem());
price = FXCollections.observableArrayList(order1.getPrice(), order2.getPrice(),
order3.getPrice(), order4.getPrice(), order5.getPrice(), order6.getPrice(),
order7.getPrice(), order8.getPrice(), order9.getPrice(), order10.getPrice());
// Create a ListView from the "book title" ObservableList
// Place the ListView into the "ListView" VBox
listView = new ListView(item);
listView.setOnMouseClicked( e -> updateSelectedOrder(e) );
vBoxListView = new VBox(listView);
vBoxListView.setPadding( new Insets(10) );
vBoxListView.setPrefWidth(200);
vBoxListView.setPrefHeight(200);
// Instantiate the GridPane
// Insert the "heading" Label into the first row
// Insert the "ListView" and "result" VBoxes into the second row
GridPane grid = new GridPane();
//LabelHeader
grid.addRow(0, lblHeader);
GridPane.setColumnSpan(lblHeader, 4);
GridPane.setHalignment(lblHeader, HPos.CENTER);
//Results & Button
grid.addRow(1, vBoxListView, sliderTipPercent, lblTip );
//List
grid.addRow(2, vBoxResults, btnCalculate);
GridPane.setColumnSpan(vBoxResults, 4);
GridPane.setHalignment(btnCalculate, HPos.RIGHT);
// Instantiate the Scene, set the properties for the Stage and set the Scene into it
// Show the PrimaryStage
Scene scene = new Scene(grid, 515, 300);
primaryStage.setTitle("Tip Calculator");
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* Updates the Label for hours worked when a MouseDragged event occurs for
* the "hours worked" Slider.
*
* @param event the MouseEvent parameter
*/
private void updateTipPercentSlider(MouseEvent e)
{
lblTip.setText( twoDecimals.format( sliderTipPercent.getValue() ) );
}
private void handleClickAction(ActionEvent e)
{
double tip = sliderTipPercent.getValue();
double priceItem = price.get(0);
double totalBill = 0;
if (tip > 0)
{
totalBill = priceItem * (8.625 / 100) + priceItem * (tip / 100);
}
lblResults.setText( String.format("Total Bill: $%.2f", totalBill) );
}
/**
* Gets the index from the Book "author" ListView control and uses it to
* access a matching element from the "book" ObservableList and displays
* that element in the "results" Label.
*
* @param event the MouseEvent parameter
*/
private void updateSelectedOrder(MouseEvent e)
{
int index = listView.getSelectionModel().getSelectedIndex();
lblResults.setText( item.get(index) );
lblResults.setText( price.get(index).toString());
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}