这是家庭作业。 JavaFX。我需要用组合框替换年份文本字段。我已经这样做了,但是现在当我单击计算时,没有任何反应。堆栈跟踪显示此行发生的错误
errorMsg += v.isInteger(yearsField.getText(),
"Years");
我认为问题是需要更改,因此它使用组合框选择。我不知道如何改变它。我尝试了yearCombo.getValue(),但这不起作用。我希望有人能指出我正确的方向。感谢任何帮助。
package com.murach.future_value;
import java.text.NumberFormat;
import java.util.Arrays;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
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 FutureValueApplication extends Application {
private TextField investmentField;
private TextField interestRateField;
private TextField yearsField;
private TextField futureValueField;
private ComboBox<Integer> yearCombo = new ComboBox<>();
private ListView<String> futureValueView = new ListView<>();
{
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Future Value Calculator");
GridPane grid = new GridPane();
grid.setAlignment(Pos.TOP_CENTER);
grid.setPadding(new Insets(25, 25, 25, 25));
grid.setHgap(10);
grid.setVgap(10);
Scene scene = new Scene(grid);
grid.add(new Label("Monthly Investment:"), 0, 0);
investmentField = new TextField();
grid.add(investmentField, 1, 0);
grid.add(new Label("Yearly Interest Rate:"), 0, 1);
interestRateField = new TextField();
grid.add(interestRateField, 1, 1);
grid.add(new Label("Years:"), 0, 2);
ComboBox<Integer> yearCombo = new ComboBox<>();
grid.add(yearCombo, 1,2);
yearCombo.setPromptText("Select Years");
Integer[] years = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
for(int year: years){
yearCombo.getItems().add(year);
}
Integer year = yearCombo.getSelectionModel().getSelectedItem();
grid.add(new Label("Future Value:"), 0, 3);
futureValueView = new ListView<>();
futureValueField = new TextField();
futureValueView.setPrefHeight(5*24);
futureValueField.setEditable(false);
grid.add(futureValueField, 1, 3);
Button calculateButton = new Button("Calculate");
calculateButton.setOnAction(event -> calculateButtonClicked());
Button exitButton = new Button("Exit");
exitButton.setOnAction(event -> exitButtonClicked());
HBox buttonBox = new HBox(10);
buttonBox.getChildren().add(calculateButton);
buttonBox.getChildren().add(exitButton);
buttonBox.setAlignment(Pos.BOTTOM_RIGHT);
grid.add(buttonBox, 0, 4, 2, 1);
primaryStage.setScene(scene);
primaryStage.show();
}
private void calculateButtonClicked() {
Validation v = new Validation();
String errorMsg = "";
errorMsg += v.isDouble(investmentField.getText(),
"Monthly Investment");
errorMsg += v.isDouble(interestRateField.getText(),
"Yearly Interest Rate");
errorMsg += v.isInteger(yearsField.getText(),
"Years");
if (errorMsg.isEmpty()) {
double investment = Double.parseDouble(
investmentField.getText());
double rate = Double.parseDouble(
interestRateField.getText());
int years = Integer.parseInt(
yearsField.getText());
ListView<String> futureValueList = calculateFutureValue(investment, rate, years);
futureValueView.getItems().setAll(futureValueList.getItems());
ListView<String> futureValue = calculateFutureValue(
investment, rate, years);
NumberFormat currency = NumberFormat.getCurrencyInstance();
futureValueField.setText(currency.format(futureValue));
} else {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText("Invalid Data");
alert.setContentText(errorMsg);
alert.showAndWait();
}
}
public ListView<String> calculateFutureValue(double investment, double rate, int years) {
double futureValue = 0.0;
ListView<String> listview = new ListView<>();
// convert yearly values to monthly values
double monthlyInterestRate = rate / 12 / 100;
int months = years * 12;
for (int i = 1; i <= months; i++) {
futureValue = (futureValue + investment)
* (1 + monthlyInterestRate);
int currentYear = (i-1/12+1);
if(i % 12 == 0){
listview.getItems().add("Year " + currentYear + ": $"+ futureValue);
}
}
return listview;
}
private void exitButtonClicked() {
System.exit(0); // 0 indicates a normal exit
}
}
我认为我需要在calculateButtonClicked方法中使用yearCombo而不是yearsField,但是当我尝试替换它时,出现错误。
考虑这些变化:
1. 将对象属性添加到
grid
,而不是局部变量。改变
ComboBox<Integer> yearCombo = new ComboBox<>();
grid.add(yearCombo, 1, 2);
到
yearCombo = new ComboBox<>();
grid.add(yearCombo, 1, 2);
2. 使用
yearCombo.getValue()
:
errorMsg += v.isInteger(String.valueOf(yearCombo.getValue()),
"Years");
if (errorMsg.isEmpty()) {
double investment = Double.parseDouble(
investmentField.getText());
double rate = Double.parseDouble(
interestRateField.getText());
int years = yearCombo.getValue();
3. 输出
futureValueView
中最后一个futureValueField
。更换
futureValueField.setText(currency.format(futureValue));
与
futureValueField.setText(futureValue.getItems().get(futureValue.getItems().size() - 1));