package pack2;
import javafx.application.Application;
import javafx.geometry.HPos;
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.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class Loan extends Application {
private TextField annualInterestRate = new TextField(); //年利息率文本域
private TextField numberOfYears = new TextField(); //年数文本域
private TextField loanAmount = new TextField(); //贷款数
private TextField monthlyPayment = new TextField(); //月支付
private TextField totalPayment = new TextField(); //总支付
private Button calculate = new Button("Calculate"); //计算按钮
@Override
public void start(Stage primaryStage) throws Exception {
GridPane pane = new GridPane(); //创建一个网格面板
pane.setAlignment(Pos.CENTER); //面板设置居中对齐
pane.setHgap(5); //面板设置水平间距为5px(像素)
pane.setVgap(5); //面板设置垂直间距为5px
//添加结点到列
pane.addColumn(0, new Label("Annual Interest Rate:"),new Label("Number of Years:"),
new Label("Loan Amount:"),new Label("Monthly Payment:"),new Label("Total Payment:"));
pane.addColumn(1, annualInterestRate,numberOfYears,loanAmount,monthlyPayment,totalPayment,calculate);
Label label = new Label(" 计算贷款\n------------------"); //创建一个标签对象
label.setFont(Font.font("华文中宋", FontWeight.BOLD, 44)); //设置标签字体(华文中宋、粗体、44px)
label.setTextFill(Color.DARKGREY); //设置文本颜色
BorderPane borderPane = new BorderPane(pane); //创建一个边框面板
borderPane.setStyle("-fx-background-color: beige; ");//面板设置样式
borderPane.setTop(label); //设置面板顶部为标签
BorderPane.setAlignment(borderPane.getTop(), Pos.BOTTOM_CENTER);//面板设置顶部的标签为下居中对齐
GridPane.setHalignment(calculate, HPos.RIGHT); //网格面板设置按钮为水平右对齐
annualInterestRate.setAlignment(Pos.BOTTOM_RIGHT); //文本域设置为底部右对齐(文本域的字符串向右边靠)
numberOfYears.setAlignment(Pos.BOTTOM_RIGHT);
loanAmount.setAlignment(Pos.BOTTOM_RIGHT);
monthlyPayment.setAlignment(Pos.BOTTOM_RIGHT);
totalPayment.setAlignment(Pos.BOTTOM_RIGHT);
monthlyPayment.setEditable(false); //设置为不可编辑,防止修改
totalPayment.setEditable(false);
calculate.setOnAction(e->computeLoan()); //按钮注册动作事件
Scene scene = new Scene(borderPane,420,350); //创建一个场景对象(需加入的结点,宽度,高度)
primaryStage.setTitle("LoanCalculator"); //舞台设置标题
primaryStage.setScene(scene); //舞台设置场景
primaryStage.setResizable(false); //舞台设置大小不可改变
primaryStage.show(); //展示舞台
}
/**
* 月利率 = 年利率/1200
* 月支付额 = 贷款数额 * 月利率/(1-1/(1+月利率)^年数*12)
* 总支付额 = 月支付额*12*年数
* */
private void computeLoan() {
try { //把文本域的数值型字符串(纯数字字符串)转换为数值
double annualRate = Double.parseDouble(annualInterestRate.getText());
double amount = Double.parseDouble(loanAmount.getText());
int year = Integer.parseInt(numberOfYears.getText());
double monthlyRate = annualRate/1200; //计算月利率 //计算月支付额
double monthlyPayment = amount*monthlyRate/(1-1/Math.pow(1+monthlyRate, year*12));
double totalPayment = monthlyPayment*12*year; //计算总支付额
//设置月支付额和总支付额文本域
this.monthlyPayment.setText(String.format("$%.2f", monthlyPayment));
this.totalPayment.setText(String.format("$%.2f", totalPayment));
}catch(NumberFormatException ex) {
//Do not any process when its are null above
}
}
public void computeLoan(TextField n1, TextField n2, TextField n3, TextField n4, TextField n5) {
try {
double annualRate = Double.parseDouble(n1.getText());
double amount = Double.parseDouble(n3.getText());
int year = Integer.parseInt(n2.getText());
double monthlyRate = annualRate/1200;
double monthlyPayment = amount*monthlyRate/(1-1/Math.pow(1+monthlyRate, year*12));
double totalPayment = monthlyPayment*12*year;
n4.setText(String.format("$%.2f", monthlyPayment));
n5.setText(String.format("$%.2f", totalPayment));
}catch(NumberFormatException ex) {
//Do not any process when its are null above
}
catch(NullPointerException ex) {
}
}
public static void main(String[] args) {
launch(args);
}
}
运行结果: