【发布时间】:2016-03-27 21:09:18
【问题描述】:
我正在使用 javafx 创建一个窗格,询问贷款金额和年数,以根据年利率从 5% 增加到 8% 以 0.125 计算每月和总还款额。
我想在初始窗格下方以表格的形式打印出来。但是,我一直试图弄清楚如何将多行文本添加到文本区域变量。
这是我的代码:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
public class LoanTable extends Application {
// create TextField and button variables
private TextField tfAmount = new TextField();
private TextField tfYears = new TextField();
private Button btCalc = new Button("Show Table");
private TextArea taResult = new TextArea();
@Override
public void start(Stage primaryStage) {
// create FlowPane and set properties
FlowPane pane = new FlowPane();
pane.setHgap(10);
tfAmount.setPrefColumnCount(3);
tfYears.setPrefColumnCount(3);
pane.getChildren().addAll(new Label("Loan Amount"), tfAmount, new Label("Number of Years"), tfYears, btCalc);
// create ScrollPane and set properties
ScrollPane scrollPane = new ScrollPane(taResult);
taResult.setEditable(false);
// create BorderPane and add FlowPane and ScrollPane
BorderPane borderPane = new BorderPane();
borderPane.setCenter(pane);
borderPane.setBottom(scrollPane);
// create and register a handler
btCalc.setOnAction(e -> calcTable());
// create a scene and place it in the stage
Scene scene = new Scene(borderPane);
primaryStage.setTitle("Loan-Table");
primaryStage.setScene(scene);
primaryStage.show();
}
// create a method to calculate monthly payment
public double calcMonthly(double interestRate) {
double loanAmount = Double.parseDouble(tfAmount.getText());
int years = Integer.parseInt(tfYears.getText());
double monthlyInterestRate = interestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, years * 12));
return monthlyPayment;
}
// create a method to calculate total payment
public double calcTotal(double interestRate, double monthlyPayment) {
int years = Integer.parseInt(tfYears.getText());
double totalPayment = monthlyPayment * years * 12;
return totalPayment;
}
private void calcTable() {
// get values from text fields
double interestRate = 5;
// display table header
taResult.setText(String.format("%13s%20s%18s\n", "Interest Rate", "Monthly Payment", "Total Payment"));
// print rest of table
for (int i = 1; i < 26; interestRate+=.125, i++) {
double x = calcMonthly(interestRate);
taResult.setText(String.format("%5.3f%19.2f%22.2f\n", interestRate, calcMonthly(interestRate), calcTotal(interestRate, x)));
}
}
public static void main(String args[]) {
Application.launch(args);
}
}
问题出现在“// 打印表格的其余部分”循环中。如何向 taResult 变量添加多行文本?当我运行这个程序时,我只得到了表格的最后一行。输入 10000(贷款金额)和 5(年数),输出为:
8.000 202.76 12165.84
但我想要的输出是:
Interest Rate Monthly Payment Total Payment
5.000 188.71 11322.74
5.125 189.29 11357.13
5.250 189.86 11391.59
5.375 190.44 11426.11
5.500 191.01 11460.70
5.625 191.59 11495.35
5.750 192.17 11530.06
5.875 192.75 11564.84
6.000 193.33 11599.68
6.125 193.91 11634.59
6.250 194.49 11669.56
6.375 195.08 11704.59
6.500 195.66 11739.69
6.625 196.25 11774.85
6.750 196.83 11810.08
6.875 197.42 11845.37
7.000 198.01 11880.72
7.125 198.60 11916.14
7.250 199.19 11951.62
7.375 199.79 11987.16
7.500 200.38 12022.77
7.625 200.97 12058.44
7.750 201.57 12094.18
7.875 202.17 12129.97
8.000 202.76 12165.84
我希望我把问题说清楚了。
【问题讨论】: