JavaFX Label create a loan calculator
import javafx.application.Application; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.GridPane; import javafx.stage.Stage; public class Main extends Application { private TextField tfInvestmentAmount = new TextField(); private TextField tfNumberOfYears = new TextField(); private TextField tfAnnualInterestRate = new TextField(); private TextField tfFutureValue = new TextField(); private Button btCalculate = new Button("Calculate"); @Override//from w ww .j a va 2 s .co m public void start(Stage primaryStage) { // Create UI GridPane pane = new GridPane(); pane.setVgap(5); pane.setHgap(5); pane.add(new Label("Investment Amount:"), 0, 0); pane.add(tfInvestmentAmount, 1, 0); pane.add(new Label("Number Of Years:"), 0, 1); pane.add(tfNumberOfYears, 1, 1); pane.add(new Label("Annual Interest Rate:"), 0, 2); pane.add(tfAnnualInterestRate, 1, 2); pane.add(new Label("Future value:"), 0, 3); pane.add(tfFutureValue, 1, 3); pane.add(btCalculate, 1, 4); // Set UI properties pane.setAlignment(Pos.CENTER); tfInvestmentAmount.setAlignment(Pos.BOTTOM_RIGHT); tfNumberOfYears.setAlignment(Pos.BOTTOM_RIGHT); tfAnnualInterestRate.setAlignment(Pos.BOTTOM_RIGHT); tfFutureValue.setAlignment(Pos.BOTTOM_RIGHT); tfFutureValue.setEditable(false); GridPane.setHalignment(btCalculate, HPos.RIGHT); pane.setPadding(new Insets(10, 10, 10, 10)); // Process events btCalculate.setOnAction(e -> futureValue()); // Create a scene and place it in the stage Scene scene = new Scene(pane); primaryStage.setTitle("java2s.com"); primaryStage.setScene(scene); primaryStage.show(); } private void futureValue() { double investmentAmount = Double.parseDouble(tfInvestmentAmount.getText()); int years = Integer.parseInt(tfNumberOfYears.getText()); double monthlyInterestRate = Double.parseDouble(tfAnnualInterestRate.getText()) / 1200; tfFutureValue.setText(String.format("$%.2f", (investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12)))); } }