Java Reference
In-Depth Information
15.7 Case Study: Loan Calculator
This case study develops a loan calculator using event-driven programming with GUI
controls.
Key
Point
Now, we will write the program for the loan-calculator problem presented at the beginning of
this chapter. Here are the major steps in the program:
1. Create the user interface, as shown in FigureĀ 15.9.
a. Create a GridPane . Add labels, text fields, and button to the pane.
b. Set the alignment of the button to the right.
2. Process the event.
Create and register the handler for processing the button-clicking action event. The
handler obtains the user input on the loan amount, interest rate, and number of years,
computes the monthly and total payments, and displays the values in the text fields.
GridPane
Text field is right aligned
Button is right aligned
F IGURE 15.9
The program computes loan payments.
The complete program is given in ListingĀ 15.6.
L ISTING 15.6
LoanCalculator.java
1 import javafx.application.Application;
2 import javafx.geometry.Pos;
3 import javafx.geometry.HPos;
4 import javafx.scene.Scene;
5 import javafx.scene.control.Button;
6 import javafx.scene.control.Label;
7 import javafx.scene.control.TextField;
8 import javafx.scene.layout.GridPane;
9 import javafx.stage.Stage;
10
11 public class LoanCalculator extends Application {
12
private TextField tfAnnualInterestRate = new TextField();
text fields
13
private TextField tfNumberOfYears = new TextField();
14
private TextField tfLoanAmount = new TextField();
15
private TextField tfMonthlyPayment = new TextField();
16
private TextField tfTotalPayment = new TextField();
17
private Button btCalculate = new Button( "Calculate" );
button
18
19 @Override // Override the start method in the Application class
20
public void start(Stage primaryStage) {
21
// Create UI
22
GridPane gridPane = new GridPane();
create a grid pane
 
 
 
Search WWH ::




Custom Search