我目前正在制作一个小费计算器而且我已经碰壁了。我的教科书 Big Java,Late Objects 没有答案。我扫描堆栈溢出和一点Reddit但我只能部分解决我的困境。我觉得好像我走在了正确的轨道上。问题在于连接到 calculateTipButton 的lambda表达式 calcTipClick 。 编辑如何使用滑块,拆分检查和checkAmtTextField中的用户输入来进行GUI的计算。遗憾
public class TipCalcApp extends Application {
// declare interface controls
Label titleLabel, checkAmtLabel, tipPercentLabel, splitLabel, tipAmtLabel;
Label totalLabel, amtPerPersonLabel;
TextField checkAmtText, tipAmtText, totalText, amtPerPersonText;
Slider tipPercentSlider;
ChoiceBox splitChoiceBox;
Button calcTipButton;
// declare a grid pane (8 rows and 2 columns)
GridPane grid;
@Override
public void start(Stage primaryStage) {
// instantiate labels and their properties
titleLabel = new Label("Tip Calculator");
titleLabel.setMaxWidth(Double.MAX_VALUE);
titleLabel.setAlignment(Pos.CENTER);
checkAmtLabel = new Label("Check Amount");
checkAmtLabel.setMaxWidth(Double.MAX_VALUE);
checkAmtLabel.setAlignment(Pos.CENTER_RIGHT);
tipPercentLabel = new Label("Tip Percent: ");
tipPercentLabel.setMaxWidth(Double.MAX_VALUE);
tipPercentLabel.setAlignment(Pos.CENTER_RIGHT);
splitLabel = new Label("Split");
splitLabel.setMaxWidth(Double.MAX_VALUE);
splitLabel.setAlignment(Pos.CENTER_RIGHT);
tipAmtLabel = new Label("Tip Amount");
tipAmtLabel.setMaxWidth(Double.MAX_VALUE);
tipAmtLabel.setAlignment(Pos.CENTER_RIGHT);
totalLabel = new Label("Total");
totalLabel.setMaxWidth(Double.MAX_VALUE);
totalLabel.setAlignment(Pos.CENTER_RIGHT);
amtPerPersonLabel = new Label("Amount Per Person");
amtPerPersonLabel.setMaxWidth(Double.MAX_VALUE);
amtPerPersonLabel.setAlignment(Pos.CENTER_RIGHT);
// instantiate text fileds and their properties
checkAmtText = new TextField();
tipAmtText = new TextField();
tipAmtText.setEditable(false);
totalText = new TextField();
totalText.setEditable(false);
amtPerPersonText = new TextField();
amtPerPersonText.setEditable(false);
// instantiate a slider and its properties
tipPercentSlider = new Slider();
tipPercentSlider.setPrefWidth(300);
tipPercentSlider.setMin(0);
tipPercentSlider.setMax(25);
tipPercentSlider.setMajorTickUnit(5);
tipPercentSlider.setMinorTickCount(0);
tipPercentSlider.setBlockIncrement(5);
tipPercentSlider.setShowTickLabels(true);
tipPercentSlider.setShowTickMarks(true);
tipPercentSlider.setSnapToTicks(true);
tipPercentSlider.setOrientation(Orientation.HORIZONTAL);
tipPercentSlider.valueProperty().addListener(
(observable, oldvalue, newvalue) ->
{
// show integer values only
tipPercentLabel.setText(String.format("Tip Percent: %2d%s", newvalue.intValue(), "%"));
});
// instantiate a choice box and its properties
splitChoiceBox = new ChoiceBox();
splitChoiceBox.getItems().addAll("1", "2", "3", "4", "5");
splitChoiceBox.setValue("1");
// instantiate a button and its properties
calcTipButton = new Button("Calculate Tip");
calcTipButton.setMaxWidth(Double.MAX_VALUE);
calcTipButton.setOnAction(e -> calcTipClick());
// instantiate a grid pane and its properties
grid = new GridPane();
grid.setHgap(15);
grid.setVgap(15);
grid.setPadding(new Insets(10));
grid.add(titleLabel, 0, 0, 2, 1);
grid.addRow(1, checkAmtLabel, checkAmtText);
grid.addRow(2, tipPercentLabel, tipPercentSlider);
grid.addRow(3, splitLabel, splitChoiceBox);
grid.add(calcTipButton, 0, 4, 2, 1);
grid.addRow(5, tipAmtLabel, tipAmtText);
grid.addRow(6, totalLabel, totalText);
grid.addRow(7, amtPerPersonLabel, amtPerPersonText);
// instantiate the grid pane and put items in in grid
Scene scene = new Scene(grid);
scene.getRoot().setStyle("-fx-font: 15 'Comic Sans MS'");
primaryStage.setTitle("Tip Calculator");
primaryStage.setScene(scene);
primaryStage.show();
}
private void calcTipClick() {
//Gather choiceBox
String choiceInput = splitChoiceBox.getValue().toString();
int choiceSelection = Integer.parseInt(choiceInput.substring(0, 1));
//Gather Slider information
String sliderInput;
sliderInput = tipPercentLabel.getValue().toString();
int sliderSelection = Integer.parseInt(sliderInput.substring(0, 1));
//Gather textField amount
}
答案 0 :(得分:0)
不确定这是否是您要问的,但这是一个如何计算提示并在calcTipClick()方法中将其拆分的想法。您应该查看格式化程序以确保格式化,舍入等。但这应该给您一般的想法。
private void calcTipClick() {
//Gather choiceBox
String choiceInput = splitChoiceBox.getValue().toString();
int choiceSelection = Integer.parseInt(choiceInput.substring(0, 1));
//Gather Slider information
Number sliderInput = tipPercentSlider.getValue();
//Gather textField amount
String val = checkAmtText.getText();
NumberStringConverter nsc = new NumberStringConverter();
Number amount = 0;
try {
amount = nsc.fromString(val);
}catch (Exception pe) {
//Need to handle a parse error if the user isn't entering numbers
//Should look at text formatters to ensure amount is only entered valid
amount = 0;
}
Number tipAmount = amount.doubleValue() * sliderInput.doubleValue()/100;
tipAmtText.setText(tipAmount.toString());
Number totalAmount = tipAmount.doubleValue() + amount.doubleValue();
totalText.setText(totalAmount.toString());
Number perPerson = totalAmount.doubleValue() / choiceSelection;
amtPerPersonText.setText(perPerson.toString());
}