(Bank Account Information Application) Create an application that allows a user to input a deposit amount (Fig. 4.15). Each time the user clicks the Enter JButton, the applica- tion adds the deposit amount entered by the user in the Deposit amount: JTextField to the balance that is currently displayed in the Balance: JTextField, then displays the new result in the Balance: JTextField. [Note: This application displays Sue Purple as a default client name and 12345 as a default account number.]
a) Copying the template to your working directory. Copy the C:Examples Tutorial04ExercisesAccountInformation directory to your C:SimplyJava directory.
b) Opening the Command Prompt window and changing directories. Open the Com- mand Prompt window by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:SimplyJava AccountInformation.
c) Compiling the template application. Compile your application by typing javac
AccountInformation.java.
d) Running the template application. Run the Bank Account Information template application by typing java AccountInformation. Type 100 in the Deposit amount: JTextField. When you press the Enter JButton, notice that the deposit amount is not added to the balance because you have not added code to the Enter JButton’s event handler yet.
e) Opening the template file. Open the AccountInformation.java file in your text editor.
f) Inserting code in the event handler. I
```
1 // AccountInformation.java
2 // This application inputs and outputs account information.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7
8 public class AccountInformation extends JFrame
9 {
10 // JPanel to group deposit components
11 private JPanel enterJPanel;
12
13 // JLabel and JTextField for deposits
14 private JLabel depositJLabel;
15 private JTextField depositJTextField;
16
17 // JButton to initiate balance calculation
18 private JButton enterJButton;
19
20 // JPanel to group account information components
21 private JPanel accountJPanel;
22
23 // JLabel and JTextField for account holder's name
24 private JLabel nameJLabel;
25 private JTextField nameJTextField;
26
27 // JLabel and JTextField for account number
28 private JLabel accountNumberJLabel;
29 private JTextField accountNumberJTextField;
30
31 // JLabel and JTextField for balance
32 private JLabel balanceJLabel;
33 private JTextField balanceJTextField;
34
35 // no-argument constructor
36 public AccountInformation()
37 {
38 createUserInterface();
39 }
40
41 // create and position GUI components; register event handlers
42 private void createUserInterface()
43 {
44 // get content pane and set layout to null
45 Container contentPane = getContentPane();
46 contentPane.setLayout( null );
47
48 // set up enterJPanel
49 enterJPanel = new JPanel();
50 enterJPanel.setLayout( null );
51 enterJPanel.setBounds( 16, 16, 152, 126 );
52 enterJPanel.setBorder(
53 new TitledBorder( "Enter information" ) );
54 contentPane.add( enterJPanel );
55
56 // set up depositJLabel
57 depositJLabel = new JLabel();
58 depositJLabel.setText( "Deposit amount:" );
59 depositJLabel.setBounds( 16, 40, 140, 16 );
60 enterJPanel.add( depositJLabel );
61
62 // set up depositJTextField
63 depositJTextField = new JTextField();
64 depositJTextField.setText( "0" );
65 depositJTextField.setBounds( 16, 56, 120, 21 );
66 depositJTextField.setHorizontalAlignment( JTextField.RIGHT );
67 enterJPanel.add( depositJTextField );
68
69 // set up enterJButton
70 enterJButton = new JButton();
71 enterJButton.setText( "Enter" );
72 enterJButton.setBounds( 16, 150, 152, 34 );
73 contentPane.add( enterJButton );
74 enterJButton.addActionListener(
75
76 new ActionListener() // anonymous inner class
77 {
78 // event handler called when enterJButton is pressed
79 public void actionPerformed( ActionEvent event )
80 {
81 enterJButtonActionPerformed( event );
82 }
83
84 } // end anonymous inner class
85
86 ); // end call to addActionListener
87
88 // set up accountJPanel
89 accountJPanel = new JPanel();
90 accountJPanel.setLayout( null );
91 accountJPanel.setBounds( 180, 16, 136, 170 );
92 accountJPanel.setBorder(
93 new TitledBorder( "Account information" ) );
94 contentPane.add( accountJPanel );
95
96 // set up nameJLabel
97 nameJLabel = new JLabel();
98 nameJLabel.setText( "Name:" );
99 nameJLabel.setBounds( 16, 24, 100, 16 );
100 accountJPanel.add( nameJLabel );
101
102 // set up nameJTextField
103 nameJTextField = new JTextField();
104 nameJTextField.setText( "Sue Purple" );
105 nameJTextField.setBounds( 16, 40, 104, 21 );
106 nameJTextField.setEditable( false );
107 accountJPanel.add( nameJTextField );
108
109 // set up accountNumberJLabel
110 accountNumberJLabel = new JLabel();
111 accountNumberJLabel.setText( "Account Number:" );
112 accountNumberJLabel.setBounds( 16, 70, 140, 16 );
113 accountJPanel.add( accountNumberJLabel );
114
115 // set up accountNumberJTextField
116 accountNumberJTextField = new JTextField();
117 accountNumberJTextField.setText( "12345" );
118 accountNumberJTextField.setBounds( 16, 86, 104, 21 );
119 accountNumberJTextField.setEditable( false );
120 accountNumberJTextField.setHorizontalAlignment(
121 JTextField.RIGHT );
122 accountJPanel.add( accountNumberJTextField );
123
124 // set up balanceJLabel
125 balanceJLabel = new JLabel();
126 balanceJLabel.setText( "Balance:" );
127 balanceJLabel.setBounds( 16, 116, 100, 16 );
128 accountJPanel.add( balanceJLabel );
129
130 // set up balanceJTextField
131 balanceJTextField = new JTextField();
132 balanceJTextField.setText( "0" );
133 balanceJTextField.setBounds( 16, 132, 104, 21 );
134 balanceJTextField.setEditable( false );
135 balanceJTextField.setHorizontalAlignment( JTextField.RIGHT );
136 accountJPanel.add( balanceJTextField );
137
138 // set properties of application’s window
139 setTitle( "Account Information" ); // set title bar text
140 setSize( 340, 225 ); // set window's size
141 setVisible( true ); // display window
142
143 } // end method createUserInterface
144
145 // add previous balance to deposit and display result
146 private void enterJButtonActionPerformed( ActionEvent event )
147 {
148 // display new balance
149 balanceJTextField.setText( String.valueOf(
150 Integer.parseInt( balanceJTextField.getText() ) +
151 Integer.parseInt( depositJTextField.getText() ) ) );
152
153 // clear depositJTextField
154 depositJTextField.setText( "0" );
155
156 } // end method enterJButtonActionPerformed
157
158 // main method
159 public static void main( String args[] )
160 {
161 AccountInformation application = new AccountInformation();
162 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
163
164 } // end method main
165
166 } // end class AccountInformation
```
You might also like to view...
You can advance slides during a slide show by pressing the PAGE DOWN key.
Answer the following statement true (T) or false (F)
On the right side of the Start menu, ________ provide quick access to the folders and features you use most often
Fill in the blank(s) with correct word
Which of the following frames does NOT correspond to user actions?
A. Over B. Up C. Hit D. Down
A(n) ____________________ is a predesigned set of formatting elements, including colors, which you can use to achieve a coordinated overall look in your document.
Fill in the blank(s) with the appropriate word(s).