(Simple Calculator Application) In this exercise, you will add functionality to a simple calculator application. The calculator will allow a user to enter two numbers in the input JTextFields. There will be four JButtons, labeled +, -, / and *. When the user clicks the JButton labeled as + (addition), - (subtraction), * (multiplication) or / (division), the applica- tion will perform that operation on the numbers in the Enter first number: and Enter second number: input JTextFields and display the resultin the Result: output JTextField. Figure 5.34 displays the completed calculator.





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

b) Opening the template file. Open the SimpleCalculator.java file in your text editor. c) Code the + JButton event handler. Scroll to the addJButtonActionPerformed

event handler (lines 215–218). After line 216 insert statements that obtain the user

input from firstNumberJTextField and secondNumberJTextField, convert those numbers to int values and assign the numbers to int variables number1 and number2, respectively. Next, insert a statement that adds the numbers and assigns the result to int variable result. Finally, insert a statement that displays the value of result in resultJTextField.

d) Code the - JButton event handler. Locate the subtractJButtonActionPerformed event handler (immediately following addJButtonActionPerformed). In the body of the event handler, insert statements that obtain the use

```
1 // SimpleCalculator.java
2 // Application that takes two numbers and performs arithmetic
3 // operations with the input values.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class SimpleCalculator extends JFrame
9 {
10 // JLabel and JTextField for user to input first number
11 private JLabel firstNumberJLabel;
12 private JTextField firstNumberJTextField;
13
14 // JLabel and JTextField for user to input second number
15 private JLabel secondNumberJLabel;
16 private JTextField secondNumberJTextField;
17
18 // JLabel and JTextField to display result
19 private JLabel resultJLabel;
20 private JTextField resultJTextField;
21
22 // JButtons to initiate calculations
23 private JButton addJButton;
24 private JButton subtractJButton;
25 private JButton multiplyJButton;
26 private JButton divideJButton;
27
28 // no-argument constructor
29 public SimpleCalculator()
30 {
31 createUserInterface();
32 }
33
34 // create and position GUI components; register event handlers
35 private void createUserInterface()
36 {
37 // get content pane for attaching GUI components
38 Container contentPane = getContentPane();
39
40 // enable explicit positioning of GUI components
41 contentPane.setLayout( null );
42
43 // set up firstNumberJLabel
44 firstNumberJLabel = new JLabel();
45 firstNumberJLabel.setText( "Enter first number:" );
46 firstNumberJLabel.setBounds( 20, 20, 130, 25 );
47 contentPane.add( firstNumberJLabel );
48
49 // set up firstNumberJTextField
50 firstNumberJTextField = new JTextField();
51 firstNumberJTextField.setText( "0" );
52 firstNumberJTextField.setBounds( 170, 20, 60, 25 );
53 firstNumberJTextField.setHorizontalAlignment(
54 JTextField.RIGHT );
55 contentPane.add( firstNumberJTextField );
56 firstNumberJTextField.addKeyListener(
57
58 new KeyAdapter() // anonymous inner class
59 {
60 // called when key pressed in firstNumberJTextField
61 public void keyPressed( KeyEvent event )
62 {
63 firstNumberJTextFieldKeyPressed( event );
64 }
65
66 } // end anonymous inner class
67
68 ); // end call to addKeyListener
69
70 // set up secondNumberJLabel
71 secondNumberJLabel = new JLabel();
72 secondNumberJLabel.setText( "Enter second number:" );
73 secondNumberJLabel.setBounds( 20, 55, 130, 25 );
74 contentPane.add( secondNumberJLabel );
75
76 // set up secondNumberJTextField
77 secondNumberJTextField = new JTextField();
78 secondNumberJTextField.setText( "0" );
79 secondNumberJTextField.setBounds( 170, 55, 60, 25 );
80 secondNumberJTextField.setHorizontalAlignment(
81 JTextField.RIGHT );
82 contentPane.add( secondNumberJTextField );
83 secondNumberJTextField.addKeyListener(
84
85 new KeyAdapter() // anonymous inner class
86 {
87 // called when key pressed in secondNumberJTextField
88 public void keyPressed( KeyEvent event )
89 {
90 secondNumberJTextFieldKeyPressed( event );
91 }
92
93 } // end anonymous inner class
94
95 ); // end call to addKeyListener
96
97 // set up resultJLabel
98 resultJLabel = new JLabel();
99 resultJLabel.setText( "Result:" );
100 resultJLabel.setBounds( 20, 90, 130, 25 );
101 contentPane.add( resultJLabel );
102
103 // set up resultJTextField
104 resultJTextField = new JTextField();
105 resultJTextField.setHorizontalAlignment( JTextField.RIGHT );
106 resultJTextField.setEditable( false );
107 resultJTextField.setBounds( 170, 90, 60, 25 );
108 contentPane.add( resultJTextField );
109
110 // set up addJButton
111 addJButton = new JButton ();
112 addJButton.setText( "+" );
113 addJButton.setBounds( 250, 20, 41, 25 );
114 addJButton.setFont( new Font( "Monospaced", Font.PLAIN, 12 ) );
115 contentPane.add( addJButton );
116 addJButton.addActionListener(
117
118 new ActionListener() // anonymous inner class
119 {
120 // called when addJButton is pressed
121 public void actionPerformed( ActionEvent event )
122 {
123 addJButtonActionPerformed( event );
124 }
125
126 } // end anonymous inner class
127
128 ); // end call to addActionListener
129
130 // set up subtractJButton
131 subtractJButton = new JButton ();
132 subtractJButton.setText( "-" );
133 subtractJButton.setBounds( 301, 20, 41, 25 );
134 subtractJButton.setFont(
135 new Font( "Monospaced", Font.PLAIN, 12 ) );
136 contentPane.add( subtractJButton );
137 subtractJButton.addActionListener(
138
139 new ActionListener() // anonymous inner class
140 {
141 // called when subtractJButton is pressed
142 public void actionPerformed( ActionEvent event )
143 {
144 subtractJButtonActionPerformed( event );
145 }
146
147 } // end anonymous inner class
148
149 ); // end call to addActionListener
150
151 // set up multiplyJButton
152 multiplyJButton = new JButton ();
153 multiplyJButton.setText( "*" );
154 multiplyJButton.setBounds( 250, 55, 41, 25 );
155 multiplyJButton.setFont(
156 new Font( "Monospaced", Font.PLAIN, 12 ) );
157 contentPane.add( multiplyJButton );
158 multiplyJButton.addActionListener(
159
160 new ActionListener() // anonymous inner class
161 {
162 // called when multiplyJButton is pressed
163 public void actionPerformed( ActionEvent event )
164 {
165 multiplyJButtonActionPerformed( event );
166 }
167
168 } // end anonymous inner class
169
170 ); // end call to addActionListener
171
172 // set up divideJButton
173 divideJButton = new JButton ();
174 divideJButton.setText( "/" );
175 divideJButton.setBounds( 301, 55, 41, 25 );
176 divideJButton.setFont(
177 new Font( "Monospaced", Font.PLAIN, 12 ) );
178 contentPane.add( divideJButton );
179 divideJButton.addActionListener(
180
181 new ActionListener() // anonymous inner class
121 public void actionPerformed( ActionEvent event )
122 {
123 addJButtonActionPerformed( event );
124 }
125
126 } // end anonymous inner class
127
128 ); // end call to addActionListener
129
130 // set up subtractJButton
131 subtractJButton = new JButton ();
132 subtractJButton.setText( "-" );
133 subtractJButton.setBounds( 301, 20, 41, 25 );
134 subtractJButton.setFont(
135 new Font( "Monospaced", Font.PLAIN, 12 ) );
136 contentPane.add( subtractJButton );
137 subtractJButton.addActionListener(
138
139 new ActionListener() // anonymous inner class
140 {
141 // called when subtractJButton is pressed
142 public void actionPerformed( ActionEvent event )
143 {
144 subtractJButtonActionPerformed( event );
145 }
146
147 } // end anonymous inner class
148
149 ); // end call to addActionListener
150
151 // set up multiplyJButton
152 multiplyJButton = new JButton ();
153 multiplyJButton.setText( "*" );
154 multiplyJButton.setBounds( 250, 55, 41, 25 );
155 multiplyJButton.setFont(
156 new Font( "Monospaced", Font.PLAIN, 12 ) );
157 contentPane.add( multiplyJButton );
158 multiplyJButton.addActionListener(
159
160 new ActionListener() // anonymous inner class
161 {
162 // called when multiplyJButton is pressed
163 public void actionPerformed( ActionEvent event )
164 {
165 multiplyJButtonActionPerformed( event );
166 }
167
168 } // end anonymous inner class
169
170 ); // end call to addActionListener
171
172 // set up divideJButton
173 divideJButton = new JButton ();
174 divideJButton.setText( "/" );
175 divideJButton.setBounds( 301, 55, 41, 25 );
176 divideJButton.setFont(
177 new Font( "Monospaced", Font.PLAIN, 12 ) );
178 contentPane.add( divideJButton );
179 divideJButton.addActionListener(
180
181 new ActionListener() // anonymous inner class
121 public void actionPerformed( ActionEvent event )
122 {
123 addJButtonActionPerformed( event );
124 }
125
126 } // end anonymous inner class
127
128 ); // end ca

Computer Science & Information Technology

You might also like to view...

The startup option where only the most basic operating system processes are started is referred to as:

A) Safe Mode B) Protected Mode C) Default Mode D) Security Mode

Computer Science & Information Technology

Which of the following is the technique for adding light and shadows to a 3D image?

A. Rendering B. Ray tracing C. Wireframe D. Rasterize

Computer Science & Information Technology