(Mortgage Calculator GUI) In this exercise, you apply the GUI design guidelines you have learned to a graphical user interface for a mortgage calculator (Fig. 3.20). You will set the bounds of the Loan amount: JLabel and JTextField such that they align properly with the other GUI components.




a) Copying the template to your working directory. Copy the C:Examples Tutorial03ExercisesMortgageCalculator directory to your C:SimplyJava directory.


b) Opening the Command Prompt window and changing directories. Open the Com- mand Prompt by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:SimplyJavaMortgageCalcula- tor, then pressing Enter.


c) Compiling the template application. Compile your application by typing javac


MortgageCalculator.java, then pressing Enter.


d) Running the template application. Run the application by typing java Mortgage- Calculator, then pressing Enter. The GUI of the Mortgage Calculator template application should appear as shown in Fig. 3.21.





e) Closing the application. Close your running application by cl


```
1 // MortgageCalculator.java
2 // GUI for mortgage calculator application.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7
8 public class MortgageCalculator extends JFrame
9 {
10 // JLabel and JTextField for home value
11 private JLabel homeValueJLabel;
12 private JTextField homeValueJTextField;
13
14 // JLabel and JTextField for loan amount
15 private JLabel loanAmountJLabel;
16 private JTextField loanAmountJTextField;
17
18 // JLabel and JTextField for mortgage term in years
19 private JLabel termJLabel;
20 private JTextField termJTextField;
21
22 // JLabel and JTextField for yearly homeowner's insurance
23 private JLabel insuranceJLabel;
24 private JTextField insuranceJTextField;
25
26 // JLabel and JTextField for yearly property taxes
27 private JLabel propertyTaxesJLabel;
28 private JTextField propertyTaxesJTextField;
29
30 // JLabel and JTextField for interest rate percentage
31 private JLabel interestJLabel;
32 private JTextField interestJTextField;
33
34 // JLabel and JTextField for monthly payment amount
35 private JLabel monthlyPaymentJLabel;
36 private JTextField monthlyPaymentJTextField;
37
38 // JButtons to calculate monthly payment and clear all JTextFields
39 private JButton calculateJButton;
40 private JButton clearJButton;
41
42 // no-argument constructor
43 public MortgageCalculator()
44 {
45 createUserInterface();
46 }
47
48 // create and position GUI components
49 public void createUserInterface()
50 {
51 // get content pane and set its layout
52 Container container = getContentPane();
53 container.setLayout( null );
54
55 // set up homeValueJLabel
56 homeValueJLabel = new JLabel();
57 homeValueJLabel.setBounds( 16, 16, 80, 21 );
58 homeValueJLabel.setText( "Home value:" );
59 container.add( homeValueJLabel );
60
61 // set up homeValueJTextField
62 homeValueJTextField = new JTextField();
63 homeValueJTextField.setBounds( 212, 16, 100, 21 );
64 homeValueJTextField.setText( "125000" );
65 homeValueJTextField.setHorizontalAlignment( JTextField.RIGHT );
66 container.add( homeValueJTextField );
67
68 // set up loanAmountJLabel
69 loanAmountJLabel = new JLabel();
70 loanAmountJLabel.setBounds( 16, 56, 80, 21 );
71 loanAmountJLabel.setText( "Loan amount:" );
72 container.add( loanAmountJLabel );
73
74 // set up loanAmountJTextField
75 loanAmountJTextField = new JTextField();
76 loanAmountJTextField.setBounds( 212, 56, 100, 21 );
77 loanAmountJTextField.setText( "100000" );
78 loanAmountJTextField.setHorizontalAlignment(
79 JTextField.RIGHT );
80 container.add( loanAmountJTextField );
81
82 // set up termJLabel
83 termJLabel = new JLabel();
84 termJLabel.setBounds( 16, 96, 80, 21 );
85 termJLabel.setText( "Term (years):" );
86 container.add( termJLabel );
87
88 // set up termJTextField
89 termJTextField = new JTextField();
90 termJTextField.setBounds( 212, 96, 100, 21 );
91 termJTextField.setText( "20" );
92 termJTextField.setHorizontalAlignment( JTextField.RIGHT );
93 container.add( termJTextField );
94
95 // set up insuranceJLabel
96 insuranceJLabel = new JLabel();
97 insuranceJLabel.setBounds( 16, 136, 196, 21 );
98 insuranceJLabel.setText( "Homeowner's insurance (yearly):" );
99 container.add( insuranceJLabel );
100
101 // set up insuranceJTextField
102 insuranceJTextField = new JTextField();
103 insuranceJTextField.setBounds( 212, 136, 100, 21 );
104 insuranceJTextField.setText( "500" );
105 insuranceJTextField.setHorizontalAlignment( JTextField.RIGHT );
106 container.add( insuranceJTextField );
107
108 // set up propertyTaxesJLabel
109 propertyTaxesJLabel = new JLabel();
110 propertyTaxesJLabel.setBounds( 16, 176, 148, 21 );
111 propertyTaxesJLabel.setText( "Property taxes (yearly):" );
112 container.add( propertyTaxesJLabel );
113
114 // set up propertyTaxesJTextField
115 propertyTaxesJTextField = new JTextField();
116 propertyTaxesJTextField.setBounds( 212, 176, 100, 21 );
117 propertyTaxesJTextField.setText( "2100" );
118 propertyTaxesJTextField.setHorizontalAlignment(
119 JTextField.RIGHT );
120 container.add( propertyTaxesJTextField );
121
122 // set up interestJLabel
123 interestJLabel = new JLabel();
124 interestJLabel.setBounds( 16, 216, 140, 21 );
125 interestJLabel.setText( "Interest rate (percent):" );
126 container.add( interestJLabel );
127
128 // set up interestJTextField
129 interestJTextField = new JTextField();
130 interestJTextField.setBounds( 212, 216, 100, 21 );
131 interestJTextField.setText( "5.5" );
132 interestJTextField.setHorizontalAlignment( JTextField.RIGHT );
133 container.add( interestJTextField );
134
135 // set up monthlyPaymentJLabel
136 monthlyPaymentJLabel = new JLabel();
137 monthlyPaymentJLabel.setBounds( 16, 256, 106, 21 );
138 monthlyPaymentJLabel.setText( "Monthly payment:" );
139 container.add( monthlyPaymentJLabel );
140
141 // set up monthlyPaymentJTextField
142 monthlyPaymentJTextField = new JTextField();
143 monthlyPaymentJTextField.setBounds( 212, 256, 100, 21 );
144 monthlyPaymentJTextField.setHorizontalAlignment(
145 JTextField.CENTER );
146 monthlyPaymentJTextField.setBorder( new BevelBorder( 1 ) );
147 monthlyPaymentJTextField.setEditable( false );
148 container.add( monthlyPaymentJTextField );
149
150 // set up calculateJButton
151 calculateJButton = new JButton();
152 calculateJButton.setBounds( 133, 296, 90, 23 );
153 calculateJButton.setText( "Calculate" );
154 container.add( calculateJButton );
155
156 // set up clearJButton
157 clearJButton = new JButton();
158 clearJButton.setBounds( 237, 296, 75, 23 );
159 clearJButton.setText( "Clear" );
160 container.add( clearJButton );
161
162 // set properties of application’s window
163 setTitle( "Mortgage Calculator" ); // set title bar text
164 setSize( 332, 360 ); // set window size
165 setVisible( true ); // display window
166
167 } // end method createUserInterface
168
169 // main method
170 public static void main( String[] args )
171 {
172 MortgageCalculator application = new MortgageCalculator();
173 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
174
175 } // end method main
176
177 } // end class MortgageCalculator
```

Computer Science & Information Technology

You might also like to view...

Which of the following is true when implementing a one-to-many relationship?

A) The file on the one end has a foreign key consisting of the primary key of the many end. B) The file on the one end has a concatenated key of its primary key and the many end key. C) The file on the many end has a foreign key consisting of the primary key on the one end. D) The file on the many end has a concatenated key of its primary key and the one end key.

Computer Science & Information Technology

A computer that receives a request and returns data is a(n) ________

Fill in the blank(s) with correct word

Computer Science & Information Technology

An optical mouse uses ________

A) coordinates B) two roller balls C) a set of rollers D) reflected beams of light

Computer Science & Information Technology

The location of the ________ in a document determines where text or a picture from a file is positioned

Fill in the blank(s) with correct word

Computer Science & Information Technology