Welcome to mirror list, hosted at ThFree Co, Russian Federation.

index.html « 2014-12-08-investment-calculator-java « post « public - github.com/brycematheson/allegiant.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70202aca255268d1dee3c7a859cc8c56cc8e1ce3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<!DOCTYPE html>
<html lang="en-us">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
  <title>Build a GUI Investment Calculator in Java &middot; Allegiant</title>
  <link rel="stylesheet" href="http://localhost:1313/brycematheson.io/css/style.css" />
  <link rel="stylesheet" href="http://localhost:1313/brycematheson.io/css/font-awesome.min.css" />
  <link href='https://fonts.googleapis.com/css?family=Lato:100,300,400,600' rel='stylesheet' type='text/css'>
  <link rel="shortcut icon" href="http://localhost:1313/brycematheson.io/img/favicon.ico" />
  <link rel="apple-touch-icon" href="http://localhost:1313/brycematheson.io/img/apple-touch-icon.jpg" />
  
</head>
<body>

<header class="header" style="background-color: #009DDC;
    background-image: url(http://localhost:1313/brycematheson.io//img/texture.png),linear-gradient(to bottom, #009DDC, #006ccb);background-repeat: repeat, no-repeat;background-position: left top, left top;background-size: 100px 100px, 100% 100%;" role="banner">

      <section id="branding">
        <div id="site-title"><a href="http://localhost:1313/brycematheson.io/">Allegiant</a></div>
        <nav id="mainmenu">
          <ul>
            <li><a href="http://localhost:1313/brycematheson.io/">Articles</a></li>
            <li><a href="https://github.com/brycematheson" target="_blank">Github</a></li>
            <li><a href="https://twitter.com/brycematheson" target="_blank">Twitter</a></li>
            <li><a href="https://www.linkedin.com/in/brycematheson" target="_blank">LinkedIn</a></li>
          </ul>
        </nav>
        <input type="checkbox" id="op"></input>
        <div class="lower">
          <label for="op">Menu</label>
        </div>
        <div class="overlay overlay-hugeinc">
          <label for="op"></label>
          <nav id="menu" role="navigation">
            <ul>
            <li><a href="http://localhost:1313/brycematheson.io/">Articles</a></li>
            <li><a href="https://github.com/brycematheson" target="_blank">Github</a></li>
            <li><a href="https://twitter.com/brycematheson" target="_blank">Twitter</a></li>
            <li><a href="https://www.linkedin.com/in/brycematheson" target="_blank">LinkedIn</a></li>
          </ul>
          </nav>
        </div>
        <div class="clearfix"></div>
      </section>
      <div class="post-heading">
        <h1>
        Build a GUI Investment Calculator in Java
          <div class="share-icons-header">
            <a href="http://www.twitter.com/share?url=http%3a%2f%2flocalhost%3a1313%2fbrycematheson.io%2fpost%2f2014-12-08-investment-calculator-java%2f" target="_blank">
            <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 612 612" style="enable-background:new 0 0 612 612;" xml:space="preserve">
            <g>
              <g>
                <path d="M437.219,245.162c0-3.088-0.056-6.148-0.195-9.18c13.214-9.848,24.675-22.171,33.744-36.275
                  c-12.129,5.453-25.148,9.097-38.835,10.626c13.965-8.596,24.675-22.338,29.738-38.834c-13.075,7.928-27.54,13.603-42.924,16.552
                  c-12.323-14.021-29.904-22.95-49.35-23.284c-37.332-0.612-67.598,30.934-67.598,70.463c0,5.619,0.584,11.072,1.752,16.329
                  c-56.22-3.616-106.042-32.881-139.369-77c-5.814,10.571-9.152,22.922-9.152,36.164c0,25.037,11.934,47.291,30.071,60.421
                  c-11.099-0.5-21.503-3.866-30.627-9.375c0,0.306,0,0.612,0,0.918c0,34.996,23.312,64.316,54.245,71.159
                  c-5.675,1.613-11.656,2.448-17.804,2.421c-4.367-0.028-8.596-0.501-12.713-1.392c8.596,28.681,33.577,49.628,63.147,50.323
                  c-23.145,19.194-52.298,30.655-83.955,30.572c-5.453,0-10.849-0.361-16.135-1.029c29.933,20.53,65.456,32.491,103.65,32.491
                  C369.23,447.261,437.219,339.048,437.219,245.162z"/>
                <path d="M612,306C612,137.004,474.995,0,306,0C137.004,0,0,137.004,0,306c0,168.995,137.004,306,306,306
                  C474.995,612,612,474.995,612,306z M27.818,306C27.818,152.36,152.36,27.818,306,27.818S584.182,152.36,584.182,306
                  S459.64,584.182,306,584.182S27.818,459.64,27.818,306z"/>
              </g>
            </svg></a>
            <a href="http://plus.google.com/share?url=http%3a%2f%2flocalhost%3a1313%2fbrycematheson.io%2fpost%2f2014-12-08-investment-calculator-java%2f" target="_blank">
            <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 612 612" style="enable-background:new 0 0 612 612;" xml:space="preserve">
              <g>
                <g>
                  <path d="M349.146,402.251c0-30.016-17.387-44.815-36.525-60.922l-15.662-12.185c-4.785-3.895-11.294-9.124-11.294-18.693
                    c0-9.57,6.537-15.662,12.184-21.309c18.249-14.354,36.526-29.571,36.526-61.756c0-33.076-20.892-50.462-30.878-58.724l0,0h26.956
                    L358.271,153h-89.603c-23.479,0-53.049,3.478-77.863,23.924c-18.693,16.079-27.818,38.278-27.818,58.279
                    c0,33.911,26.093,68.294,72.188,68.294c4.368,0,9.125-0.445,13.937-0.863c-2.17,5.23-4.34,9.569-4.34,16.97
                    c0,13.464,6.955,21.753,13.047,29.57c-19.556,1.308-56.109,3.478-83.065,20.001c-25.676,15.217-33.493,37.416-33.493,53.077
                    c0,32.186,30.461,62.201,93.525,62.201C309.561,484.454,349.146,443.116,349.146,402.251z M255.621,291.34
                    c-37.415,0-54.384-48.292-54.384-77.418c0-11.322,2.17-23.034,9.569-32.186c6.955-8.707,19.139-14.382,30.461-14.382
                    c36.108,0,54.802,48.738,54.802,80.033c0,7.845-0.862,21.754-10.877,31.768C278.237,286.11,266.498,291.312,255.621,291.34z
                     M256.066,466.177c-46.54,0-76.556-22.171-76.556-53.049s27.846-41.311,37.416-44.787c18.276-6.093,41.755-6.982,45.677-6.982
                    c4.34,0,6.51,0,9.987,0.445c33.076,23.479,47.402,35.218,47.402,57.416C319.992,446.176,297.821,466.177,256.066,466.177z"/>
                  <polygon points="353.068,317.768 400.164,317.768 400.164,364.836 423.699,364.836 423.699,317.768 470.768,317.768 
                    470.768,294.233 423.699,294.233 423.699,247.165 400.164,247.165 400.164,294.233 353.068,294.233     "/>
                  <path d="M612,306C612,137.004,474.995,0,306,0C137.004,0,0,137.004,0,306c0,168.995,137.004,306,306,306
                    C474.995,612,612,474.995,612,306z M27.818,306C27.818,152.36,152.36,27.818,306,27.818S584.182,152.36,584.182,306
                    S459.64,584.182,306,584.182S27.818,459.64,27.818,306z"/>
                </g>
            </svg></a>
            <a href="https://www.facebook.com/sharer/sharer.php?u=http%3a%2f%2flocalhost%3a1313%2fbrycematheson.io%2fpost%2f2014-12-08-investment-calculator-java%2f" target="_blank">
            <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
               viewBox="0 0 612 612" style="enable-background:new 0 0 612 612;" xml:space="preserve">
            <g>
              <g>
                <path d="M612,306C612,137.004,474.995,0,306,0C137.004,0,0,137.004,0,306c0,168.995,137.004,306,306,306
                  C474.995,612,612,474.995,612,306z M27.818,306C27.818,152.36,152.36,27.818,306,27.818S584.182,152.36,584.182,306
                  S459.64,584.182,306,584.182S27.818,459.64,27.818,306z"/>
                <path d="M317.739,482.617V306h58.279l9.208-58.529h-67.487v-29.348c0-15.272,5.007-29.849,26.928-29.849h43.813v-58.418h-62.201
                  c-52.298,0-66.569,34.438-66.569,82.175v35.413h-35.885V306h35.885v176.617H317.739L317.739,482.617z"/>
              </g>
            </svg>
            </a>
          </div>
          
        </h1>
      </div>

    </header>

    <div id="container">


<section id="content" role="main">
<section class="entry-meta">
  <span class="post-date">Dec 8, 2014</span>
</section></header>
<section class="entry-content">
<article>
<p>I’m not a programmer. Some people just have that natural itch to want to go out and code all day long, but that isn’t me. I enjoy scripting and web development, but deep, low-level programming just simply isn’t my thing. So the fact that even <em>I</em> was able to create a simple GUI application should tell you how easy it is. The code below might look daunting at first, but we’ll walk through it piece by piece to hopefully clear things up. I’ll try to make it as easy as possible to understand, but you should have at least a <em>basic</em> understanding of programming to go through this.</p>

<p>Java makes creating applications really simple. I’ve dabbled with C++, but never stuck with it long enough to get into the Object Oriented pieces of it. Whenever learning a new programming language, it seems the first program you write is “Hello World.” The next program after that is a basic investment calculator, and so we’re going to continue that pattern.</p>

<p>When we’re finished, we’ll have a program that looks something like this:</p>

<p><img src="http://localhost:1313/brycematheson.io/img/post_images/guicalculator.png" alt="GUI Investment Calculator" /></p>

<p>At the end of this post, I’ll show the complete code. If you have the Java JDK (a free download, <a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" target="_blank">here</a>), you can easily import this code into a text editor, save it with a .java extension, and then compile it using the command line. Something this would work:</p>

<pre><code class="language-java">javac SimpleGUIProgram.java
java SimpleGUIProgram
</code></pre>

<p>Personally, I use Eclipse. It’s a <a href="https://eclipse.org/downloads/packages/eclipse-ide-java-developers/keplersr1" target="_blank">free IDE that you can download</a>, if you’re not interested in using a text editor.</p>

<p>Let’s get started.</p>

<p>First, let’s import some libraries. Libraries are basically a repository of reusable code. Rather than having to rewrite code over and over, why don’t we write it once, store it in the Java API, and then import it into our program whenever we need it? Luckily, someone has already done that for us! The specifics of each of these libraries isn’t super important, but just know that these include the graphical components that we’ll need to build our program.</p>

<pre><code class="language-java">import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
</code></pre>

<p>Next, we’ll create the text fields and buttons that will be used in our program. Notice in the image above that we have four input fields (Investment Amount, Years, Annual Interest Rate, and Future Value) and two buttons (Compute and Reset). They’re pretty easy to define in Java.</p>

<p>You might wonder what <span class="smallcode">private</span> means. Whenever you create an object and set it to private, that means that it can only be accessed within that same class. We could change it to <span class="smallcode">public</span> or even <span class="smallcode">protected</span>, but for this example, we’ll leave it how it is. There are some benefits to leaving things private, such as avoiding data corruption and a few other things, but that’s outside the scope of this tutorial.</p>

<p>Then we need to tell Java what kind of object we’ll be creating. We’ll be using <span class="smallcode">JTextField</span>, which is just an input box, and <span class="smallcode">JButton</span>, which, obviously, is a button. And after that, we just need to name our object. For the sake of being descriptive, I usually like to prefix my object names with <span class="smallcode">jtf</span> or <span class="smallcode">jb</span> or similar, so I know what it is quickly by looking at it.</p>

<pre><code class="language-java">private JTextField jtfInvestmentAmount;
private JTextField jtfAnnualInterestRate;
private JTextField jtfNumberOfYears;
private JTextField jtfFutureValue;
private JButton jbtCompute;
private JButton jbtReset;
</code></pre>

<p>Next, we’re going to define the properties for the frame (the program window). The <span class="smallcode">setTitle</span> property allows us to name the frame. You can see in the image above that the title of the window is “Loan Calculator” as we’ve defined here. You can name it whatever you’d like.</p>

<p>Next, we define the default close operation. This tells the computer what to do whenever the program is closed. We have the option to let the program to continue running, even when the box is closed. In this example, when the user hits the “x” in the corner of the window, we want our program to terminate, so we’ll use <span class="smallcode">setDefaultCloseOperation(Frame.EXIT_ON_CLOSE)</span>.</p>

<p><span class="smallcode">setSize</span> defines the window width and height in pixels. Pretty self-explanatory.</p>

<p><span class="smallcode">setLocationRelativeTo(null)</span> allows us to center the window right in the middle of the screen. In my opinion, that’s the easiest and most user friendly. You have the option to set that location in pixels, using x and y coordinates, but generally, I’ve found that the user expects the window to open in the center of the screen.</p>

<p>Last, we want to set the layout. Java uses things called “Layout Managers”, which basically defines how elements are laid out inside of the program window. There are three layout managers that are most commonly used: Border, Flow, and Grid. You can read up on those other two, as I’m not going to explain them, but in this program, we’re going to be using the grid layout manager. It allows us to define how many rows, and how many columns we want for our program. <span class="smallcode">GridLayout(5,2,5,5)</span> tells our program that we want to use the Grid Layout Manager, with five rows, two columns and then 5 pixels for spacing.</p>

<pre><code class="language-java">setTitle(&quot;Loan Calculator&quot;);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,300);
setLocationRelativeTo(null);
setLayout(new GridLayout(5,2,5,5));
</code></pre>

<p>Now that we’ve defined our window, we need to create some text labels next to our input fields. Because it’s just text, we don’t use <span class="smallcode">private</span> or <span class="smallcode">public</span> on these objects. We use the following code to create a new label (JLabel) object. Again, when we name the object, it’s good practice to use a prefix so you can easily tell what type of object it is. I use <span class="smallcode">jl</span> as my prefix. Then, we create the string of text for what the JLabel will output.</p>

<p>If you notice, I’ve placed a preceding space in front of the text. I find that Java likes to push things right up against the frame, and it’s not very aesthetically pleasing. By adding a space, it allows us to push the text away from the left edge ever-so-slightly. You can add more spacing if you prefer.</p>

<pre><code class="language-java">JLabel jlInvestmentAmount = new JLabel(&quot; Investment Amount&quot;);
JLabel jlNumberOfYears = new JLabel(&quot; Number of Years&quot;);
JLabel jlAnnualInterestRate = new JLabel(&quot; Annual Interest Rate&quot;);
JLabel jlFutureValue = new JLabel(&quot; Future Value&quot;);
</code></pre>

<p>Earlier, we defined our textfields and buttons, but we didn’t actually create them. We basically told the program, “Hey, I’m going to be making these soon. Just wanted to give you a heads up.” But now we create them.</p>

<p>In the last line of code, we write <span class="smallcode">jtfFutureValue.setEditable(false)</span>. I wanted the first three fields to be able to accept input from the user. But the last field (the Future Value field) is going to simply output our final answer. Rather than making the user confused by thinking they need to input something, I set the editing to <span class="smallcode">false</span>.</p>

<pre><code class="language-java">jtfInvestmentAmount = new JTextField();
jtfNumberOfYears = new JTextField();
jtfAnnualInterestRate = new JTextField();
jtfFutureValue = new JTextField();
jbtCompute = new JButton(&quot;Compute&quot;);
jbtReset = new JButton(&quot;Reset&quot;);
jtfFutureValue.setEditable(false);
</code></pre>

<p>Now that we’ve defined and created everything, we should be good right? Not quite. If were to run the program right now, our program wouldn’t do anything. What happened to all the things we created, you might ask? We’ve created all the objects, but we haven’t told the program to add them to our frame (window). We can do this simply by typing <span class="smallcode">add</span> and then the name of the object.</p>

<pre><code class="language-java">add (jlInvestmentAmount);
add (jtfInvestmentAmount);
add (jlNumberOfYears);
add (jtfNumberOfYears);
add (jlAnnualInterestRate);
add (jtfAnnualInterestRate);
add (jlFutureValue);
add (jtfFutureValue);
add (jbtCompute);
add (jbtReset);
</code></pre>

<p>Next, we need to tell the computer that we want it to do something once we click on a button. This code doesn’t do much else, besides call other methods (functions) that we’ll define in a bit.</p>

<pre><code class="language-java">ListenerClass listener = new ListenerClass();
jbtCompute.addActionListener(listener);
jbtReset.addActionListener(listener);
</code></pre>

<p>Another very important piece: We need to set the window visibility to <span class="smallcode">true</span>. Otherwise, nothing will appear when we run the program.</p>

<pre><code class="language-java">setVisible(true);
</code></pre>

<p>Every program in Java requires a <span class="smallcode">main</span> method. All this code does is calls the class that we’ve already created. It doesn’t look like much, but our program won’t run at all if we don’t have this.</p>

<pre><code class="language-java">public static void main(String[] args) {
new SimpleGUIProgram();
</code></pre>

<p>The following code does two important things: catches exceptions, and does all the math calculations for our program.</p>

<p>First, we need to assume that people are stupid. You would think that most people would be smart enough to input numbers into the fields of our program, but what happens if they entered a word instead of a number? Without a “try…catch”, the program would just crash, without any warning to our user. Instead, we can make things a little bit more user friendly and say, “Please enter numeric values” if they enter anything except numbers.</p>

<p>And second, the rest of this code takes the data entered by the user in the fields, does all of the calculations, and then formats it to look pretty. We pull the data from the fields, parse it into a number (either an Integer or a Double), and then convert the annual interest rate into a monthly interest rate. We could do that by dividing by 12, but I also combined the percentage into that calculation as well (1200 instead of 12) so that I don’t have to write another line of code to divide the percent by 100 as well.</p>

<pre><code class="language-java">private void computeValue() {
try {
double annualInterestRate = Double.parseDouble(jtfAnnualInterestRate.getText());
double monthlyInterestRate = annualInterestRate / 1200.0;
int NumberOfYears = Integer.parseInt(jtfNumberOfYears.getText());
double investmentAmount = Double.parseDouble(jtfInvestmentAmount.getText());
double futureValue = investmentAmount * Math.pow(1.0 + monthlyInterestRate, NumberOfYears * 12);
jtfFutureValue.setText(String.format(&quot;%.2f&quot;, futureValue));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, &quot; Please enter numeric values.&quot;);
}
}
</code></pre>

<p>This code is very simple. Whenever a user clicks on the “Reset” button, it will call this method, and change whatever is input into the boxes into a blank string.</p>

<pre><code class="language-java">private void resetForm() {
jtfInvestmentAmount.setText(&quot;&quot;);
jtfAnnualInterestRate.setText(&quot;&quot;);
jtfNumberOfYears.setText(&quot;&quot;);
jtfFutureValue.setText(&quot;&quot;);
}
</code></pre>

<p>And finally, this code is what links the buttons to the methods we created. We create a ListenerClass, which essentially says, “When I do this, do this.” So, “when I click on a button, call this method.”</p>

<pre><code class="language-java">private class ListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtCompute) {
computeValue();
}
if (e.getSource() == jbtReset) {
resetForm();
}
</code></pre>

<p>And really, that’s it! Not to shabby for ~100 lines of code, eh? Here’s the entire code:</p>

<pre><code class="language-java">/*************************
* Created by: Bryce Matheson
* Website: blog.mathesondigital.com
* Date: 12/7/2014
*
* Purpose: A simple GUI Investment Calculator
*
* Rights: Free to use for Personal Use
*************************/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SimpleGUIProgram extends JFrame {
    
    private JTextField jtfInvestmentAmount;
    private JTextField jtfAnnualInterestRate;
    private JTextField jtfNumberOfYears;
    private JTextField jtfFutureValue;
    private JButton jbtCompute;
    private JButton jbtReset;
    
    public SimpleGUIProgram() {
        
        setTitle(&quot;Loan Calculator&quot;);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(500,300);
        setLocationRelativeTo(null);
        setLayout(new GridLayout(5,2,5,5));
        
        JLabel jlInvestmentAmount = new JLabel(&quot; Investment Amount&quot;);
        JLabel jlNumberOfYears = new JLabel(&quot; Number of Years&quot;);
        JLabel jlAnnualInterestRate = new JLabel(&quot; Annual Interest Rate&quot;);
        JLabel jlFutureValue = new JLabel(&quot; Future Value&quot;);
        
        jtfInvestmentAmount = new JTextField();
        jtfNumberOfYears = new JTextField();
        jtfAnnualInterestRate = new JTextField();
        jtfFutureValue = new JTextField();
        jtfFutureValue.setEditable(false);
        
        jbtCompute = new JButton(&quot;Compute&quot;);
        jbtReset = new JButton(&quot;Reset&quot;);
        
        add (jlInvestmentAmount);
        add (jtfInvestmentAmount);
        add (jlNumberOfYears);
        add (jtfNumberOfYears);
        add (jlAnnualInterestRate);
        add (jtfAnnualInterestRate);
        add (jlFutureValue);
        add (jtfFutureValue);
        add (jbtCompute);
        add (jbtReset);
        
        ListenerClass listener = new ListenerClass();
        jbtCompute.addActionListener(listener);
        jbtReset.addActionListener(listener);
        
        setVisible(true);
    }
    
    public static void main(String[] args) {
        new SimpleGUIProgram();
    }

    private void computeValue() {
        try {
            double annualInterestRate = Double.parseDouble(jtfAnnualInterestRate.getText());
            double monthlyInterestRate = annualInterestRate / 1200.0;
            int NumberOfYears = Integer.parseInt(jtfNumberOfYears.getText());
            double investmentAmount = Double.parseDouble(jtfInvestmentAmount.getText());
            double futureValue = investmentAmount * Math.pow(1.0 + monthlyInterestRate, NumberOfYears * 12);
            jtfFutureValue.setText(String.format(&quot;%.2f&quot;, futureValue));
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, &quot; Please enter numeric values.&quot;);
        }
    }
    
    private void resetForm() {
        jtfInvestmentAmount.setText(&quot;&quot;);
        jtfAnnualInterestRate.setText(&quot;&quot;);
        jtfNumberOfYears.setText(&quot;&quot;);
        jtfFutureValue.setText(&quot;&quot;);
    }
    
    private class ListenerClass implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jbtCompute) {
                computeValue();
            }
            if (e.getSource() == jbtReset) {
                resetForm();
            }
        }
    }
}
</code></pre>

</section>
<div class="entry-links"></div>
</article>
<div class="share-icons-body">
  <a href="http://www.twitter.com/share?url=http%3a%2f%2flocalhost%3a1313%2fbrycematheson.io%2fpost%2f2014-12-08-investment-calculator-java%2f" target="_blank">
  <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 612 612" style="enable-background:new 0 0 612 612;" xml:space="preserve">
  <g>
    <g>
      <path d="M437.219,245.162c0-3.088-0.056-6.148-0.195-9.18c13.214-9.848,24.675-22.171,33.744-36.275
        c-12.129,5.453-25.148,9.097-38.835,10.626c13.965-8.596,24.675-22.338,29.738-38.834c-13.075,7.928-27.54,13.603-42.924,16.552
        c-12.323-14.021-29.904-22.95-49.35-23.284c-37.332-0.612-67.598,30.934-67.598,70.463c0,5.619,0.584,11.072,1.752,16.329
        c-56.22-3.616-106.042-32.881-139.369-77c-5.814,10.571-9.152,22.922-9.152,36.164c0,25.037,11.934,47.291,30.071,60.421
        c-11.099-0.5-21.503-3.866-30.627-9.375c0,0.306,0,0.612,0,0.918c0,34.996,23.312,64.316,54.245,71.159
        c-5.675,1.613-11.656,2.448-17.804,2.421c-4.367-0.028-8.596-0.501-12.713-1.392c8.596,28.681,33.577,49.628,63.147,50.323
        c-23.145,19.194-52.298,30.655-83.955,30.572c-5.453,0-10.849-0.361-16.135-1.029c29.933,20.53,65.456,32.491,103.65,32.491
        C369.23,447.261,437.219,339.048,437.219,245.162z"/>
      <path d="M612,306C612,137.004,474.995,0,306,0C137.004,0,0,137.004,0,306c0,168.995,137.004,306,306,306
        C474.995,612,612,474.995,612,306z M27.818,306C27.818,152.36,152.36,27.818,306,27.818S584.182,152.36,584.182,306
        S459.64,584.182,306,584.182S27.818,459.64,27.818,306z"/>
    </g>
  </svg></a>
  <a href="http://plus.google.com/share?url=http%3a%2f%2flocalhost%3a1313%2fbrycematheson.io%2fpost%2f2014-12-08-investment-calculator-java%2f" target="_blank">
  <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 612 612" style="enable-background:new 0 0 612 612;" xml:space="preserve">
    <g>
      <g>
        <path d="M349.146,402.251c0-30.016-17.387-44.815-36.525-60.922l-15.662-12.185c-4.785-3.895-11.294-9.124-11.294-18.693
          c0-9.57,6.537-15.662,12.184-21.309c18.249-14.354,36.526-29.571,36.526-61.756c0-33.076-20.892-50.462-30.878-58.724l0,0h26.956
          L358.271,153h-89.603c-23.479,0-53.049,3.478-77.863,23.924c-18.693,16.079-27.818,38.278-27.818,58.279
          c0,33.911,26.093,68.294,72.188,68.294c4.368,0,9.125-0.445,13.937-0.863c-2.17,5.23-4.34,9.569-4.34,16.97
          c0,13.464,6.955,21.753,13.047,29.57c-19.556,1.308-56.109,3.478-83.065,20.001c-25.676,15.217-33.493,37.416-33.493,53.077
          c0,32.186,30.461,62.201,93.525,62.201C309.561,484.454,349.146,443.116,349.146,402.251z M255.621,291.34
          c-37.415,0-54.384-48.292-54.384-77.418c0-11.322,2.17-23.034,9.569-32.186c6.955-8.707,19.139-14.382,30.461-14.382
          c36.108,0,54.802,48.738,54.802,80.033c0,7.845-0.862,21.754-10.877,31.768C278.237,286.11,266.498,291.312,255.621,291.34z
           M256.066,466.177c-46.54,0-76.556-22.171-76.556-53.049s27.846-41.311,37.416-44.787c18.276-6.093,41.755-6.982,45.677-6.982
          c4.34,0,6.51,0,9.987,0.445c33.076,23.479,47.402,35.218,47.402,57.416C319.992,446.176,297.821,466.177,256.066,466.177z"/>
        <polygon points="353.068,317.768 400.164,317.768 400.164,364.836 423.699,364.836 423.699,317.768 470.768,317.768 
          470.768,294.233 423.699,294.233 423.699,247.165 400.164,247.165 400.164,294.233 353.068,294.233     "/>
        <path d="M612,306C612,137.004,474.995,0,306,0C137.004,0,0,137.004,0,306c0,168.995,137.004,306,306,306
          C474.995,612,612,474.995,612,306z M27.818,306C27.818,152.36,152.36,27.818,306,27.818S584.182,152.36,584.182,306
          S459.64,584.182,306,584.182S27.818,459.64,27.818,306z"/>
      </g>
  </svg></a>
  <a href="https://www.facebook.com/sharer/sharer.php?u=http%3a%2f%2flocalhost%3a1313%2fbrycematheson.io%2fpost%2f2014-12-08-investment-calculator-java%2f" target="_blank">
  <svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 612 612" style="enable-background:new 0 0 612 612;" xml:space="preserve">
  <g>
    <g>
      <path d="M612,306C612,137.004,474.995,0,306,0C137.004,0,0,137.004,0,306c0,168.995,137.004,306,306,306
        C474.995,612,612,474.995,612,306z M27.818,306C27.818,152.36,152.36,27.818,306,27.818S584.182,152.36,584.182,306
        S459.64,584.182,306,584.182S27.818,459.64,27.818,306z"/>
      <path d="M317.739,482.617V306h58.279l9.208-58.529h-67.487v-29.348c0-15.272,5.007-29.849,26.928-29.849h43.813v-58.418h-62.201
        c-52.298,0-66.569,34.438-66.569,82.175v35.413h-35.885V306h35.885v176.617H317.739L317.739,482.617z"/>
    </g>
  </svg>
  </a>
</div>
<div class="clearfix"></div>
<figure class="author-bio">
<img class="bio-image" src="../../img/avatar.jpg" />
<figcaption class="bio-text">Bryce Matheson works as a Windows Systems Administrator for a small IT company based out of Idaho Falls. When not consumed by work, Bryce enjoys dabbling with web development and whatever other technologies are currently tickling his fancy. This site is a place to document his learning.</figcaption>
</figure>

</section>
<div class="clear"></div>
</div>
<footer id="footer" style="color: #7A7B7C; background-color: #3A3B3C;background-image: url(http://localhost:1313/brycematheson.io//img/texture.png),linear-gradient(to bottom, #3A3B3C, #1d1e1e); background-repeat: repeat, no-repeat; background-position: left top, left top; background-size: 100px 100px, 100% 100%;">
  <div id="footer-container">
    <div class="footer-column">

    </div>
    <div class="footer-column">

    </div>
    <div class="footer-column">

    </div>
  <div class="clearfix"></div>
  </div>
  <div id="copyright">&copy; Allegiant. All Rights Reserved.</div>
</footer>
<script src="http://localhost:1313/brycematheson.io/js/highlight.pack.js"></script>
  <script>
    hljs.initHighlightingOnLoad();
  </script>
  
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    ga('create', 'Your Google Analytics tracking id', 'auto');
    ga('send', 'pageview');
  </script>
  
</div>
<script data-no-instant>document.write('<script src="http://'
        + (location.host || 'localhost').split(':')[0]
		+ ':1313/livereload.js?mindelay=10"></'
        + 'script>')</script></body>
</html>