2016-03-27 3 views
1

Ich benutze javafx, um einen Bereich zu erstellen, der nach Darlehensbetrag und Anzahl der Jahre fragt, um zu berechnen, wie hoch die monatliche und gesamte Zahlung auf einem jährlichen Zinssatz von 0,125 von 5% auf 8% basiert.Wie fügt man mit javafx mehrere Textzeilen zu einem Textbereich hinzu?

Ich möchte dies in Form einer Tabelle unter dem ersten Bereich ausdrucken. Ich stehe jedoch fest, um herauszufinden, wie mehrere Textzeilen zu einer Textbereichsvariablen hinzugefügt werden.

Hier ist mein Code:

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); 

    } 

} 

Das Problem kommt bei der "// print Rest der Tabelle" Schleife. Wie füge ich mehrere Zeilen Text zur Variablen taResult hinzu? Wenn ich dieses Programm starte, lande ich einfach mit der letzten Zeile der Tabelle. Mit Eingängen 10000 (für Darlehensbetrag) und 5 (für Anzahl der Jahre) ist die Ausgabe:

8.000    202.76    12165.84 

Aber meine gewünschte Ausgabe ist:

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 

ich Ich hoffe, das Problem deutlich gemacht.

Antwort

3

Ah, vergiss es. Ich habe es herausgefunden.

Ich brauchte taResult.appendText --- .appendText ist, was ich gesucht habe.

Entschuldigung für die Verschwendung jeder Zeit, die dies las.

Verwandte Themen