0% found this document useful (0 votes)
137 views7 pages

Java GUI Calculator Code

This document contains the code for a Java calculator application. It defines classes and methods to create a graphical user interface with buttons for numbers and arithmetic operations. When the user presses buttons, numbers are displayed in a text field and calculations can be performed.

Uploaded by

abdoua201972
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views7 pages

Java GUI Calculator Code

This document contains the code for a Java calculator application. It defines classes and methods to create a graphical user interface with buttons for numbers and arithmetic operations. When the user presses buttons, numbers are displayed in a text field and calculations can be performed.

Uploaded by

abdoua201972
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DemoCalculator.

java

1 package demoCalculator;
2
3 import java.awt.EventQueue;
12
13 public class DemoCalculator {
14
15 private JFrame frame;
16 private JTextField txtDisplay;
17
18 double firstnum;
19 double secondnum;
20 double result;
21 String operations;
22 String answer;
23
24 /**
25 * Launch the application.
26 */
27 public static void main(String[] args) {
28 EventQueue.invokeLater(new Runnable() {
29 public void run() {
30 try {
31 DemoCalculator window = new DemoCalculator();
32 window.frame.setVisible(true);
33 } catch (Exception e) {
34 e.printStackTrace();
35 }
36 }
37 });
38 }
39
40 /**
41 * Create the application.
42 */
43 public DemoCalculator() {
44 initialize();
45 }
46
47 /**
48 * Initialize the contents of the frame.
49 */
50 private void initialize() {
51 frame = new JFrame();
52 frame.setBounds(100, 100, 467, 666);
53 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
54 frame.getContentPane().setLayout(null);
55
56 txtDisplay = new JTextField();
57 txtDisplay.setHorizontalAlignment(SwingConstants.RIGHT);
58 txtDisplay.setFont(new Font("Tahoma", Font.BOLD, 60));
59 txtDisplay.setBounds(10, 10, 430, 60);
60 frame.getContentPane().add(txtDisplay);
61 txtDisplay.setColumns(10);
62
63 //===================ROW 1=============================
64
65 JButton btnbackspace = new JButton("\uF0E7");

Page 1
DemoCalculator.java

66 btnbackspace.addActionListener(new ActionListener() {
67 public void actionPerformed(ActionEvent arg0) {
68
69 if(txtDisplay.getText().length()>0)
70 {
71 String backspace = null;
72
73 StringBuilder strB = new StringBuilder(txtDisplay.getText());
74 strB.deleteCharAt(txtDisplay.getText().length()-1);
75 backspace = strB.toString();
76 txtDisplay.setText(backspace);
77 }
78
79 }
80 });
81 btnbackspace.setFont(new Font("Dialog", Font.BOLD, 50));
82 btnbackspace.setBounds(10, 80, 100, 100);
83 frame.getContentPane().add(btnbackspace);
84
85 JButton btnclear = new JButton("C");
86 btnclear.addActionListener(new ActionListener() {
87 public void actionPerformed(ActionEvent arg0) {
88
89 txtDisplay.setText(null);
90
91 }
92 });
93 btnclear.setFont(new Font("Tahoma", Font.BOLD, 70));
94 btnclear.setBounds(120, 80, 100, 100);
95 frame.getContentPane().add(btnclear);
96
97 JButton btnModulus = new JButton("%");
98 btnModulus.addActionListener(new ActionListener() {
99 public void actionPerformed(ActionEvent arg0) {
100
101 firstnum = Double.parseDouble(txtDisplay.getText());
102 txtDisplay.setText("");
103 operations = "%";
104
105 }
106 });
107 btnModulus.setFont(new Font("Tahoma", Font.BOLD, 50));
108 btnModulus.setBounds(230, 80, 100, 100);
109 frame.getContentPane().add(btnModulus);
110
111 JButton btnPlus = new JButton("+");
112 btnPlus.addActionListener(new ActionListener() {
113 public void actionPerformed(ActionEvent arg0) {
114
115 firstnum = Double.parseDouble(txtDisplay.getText());
116 txtDisplay.setText("");
117 operations = "+";
118
119 }
120 });
121 btnPlus.setFont(new Font("Tahoma", Font.BOLD, 70));
122 btnPlus.setBounds(340, 80, 100, 100);

Page 2
DemoCalculator.java

123 frame.getContentPane().add(btnPlus);
124
125 //===================ROW 2=============================
126
127 JButton btn7 = new JButton("7");
128 btn7.addActionListener(new ActionListener() {
129 public void actionPerformed(ActionEvent arg0) {
130
131 String EnterValue = txtDisplay.getText() + btn7.getText();
132 txtDisplay.setText(EnterValue);
133
134 }
135 });
136 btn7.setFont(new Font("Tahoma", Font.BOLD, 70));
137 btn7.setBounds(10, 190, 100, 100);
138 frame.getContentPane().add(btn7);
139
140 JButton btn8 = new JButton("8");
141 btn8.addActionListener(new ActionListener() {
142 public void actionPerformed(ActionEvent arg0) {
143
144 String EnterValue = txtDisplay.getText() + btn8.getText();
145 txtDisplay.setText(EnterValue);
146
147 }
148 });
149 btn8.setFont(new Font("Tahoma", Font.BOLD, 70));
150 btn8.setBounds(120, 190, 100, 100);
151 frame.getContentPane().add(btn8);
152
153 JButton btn9 = new JButton("9");
154 btn9.addActionListener(new ActionListener() {
155 public void actionPerformed(ActionEvent arg0) {
156
157 String EnterValue = txtDisplay.getText() + btn9.getText();
158 txtDisplay.setText(EnterValue);
159
160 }
161 });
162 btn9.setFont(new Font("Tahoma", Font.BOLD, 70));
163 btn9.setBounds(230, 190, 100, 100);
164 frame.getContentPane().add(btn9);
165
166 JButton btnMinus = new JButton("-");
167 btnMinus.addActionListener(new ActionListener() {
168 public void actionPerformed(ActionEvent arg0) {
169
170 firstnum = Double.parseDouble(txtDisplay.getText());
171 txtDisplay.setText("");
172 operations = "-";
173
174 }
175 });
176 btnMinus.setFont(new Font("Tahoma", Font.BOLD, 70));
177 btnMinus.setBounds(340, 190, 100, 100);
178 frame.getContentPane().add(btnMinus);
179

Page 3
DemoCalculator.java

180 //===================ROW 3=============================


181
182 JButton btn4 = new JButton("4");
183 btn4.addActionListener(new ActionListener() {
184 public void actionPerformed(ActionEvent arg0) {
185
186 String EnterValue = txtDisplay.getText() + btn4.getText();
187 txtDisplay.setText(EnterValue);
188
189 }
190 });
191 btn4.setFont(new Font("Tahoma", Font.BOLD, 70));
192 btn4.setBounds(10, 300, 100, 100);
193 frame.getContentPane().add(btn4);
194
195 JButton btn5 = new JButton("5");
196 btn5.addActionListener(new ActionListener() {
197 public void actionPerformed(ActionEvent arg0) {
198
199 String EnterValue = txtDisplay.getText() + btn5.getText();
200 txtDisplay.setText(EnterValue);
201
202 }
203 });
204 btn5.setFont(new Font("Tahoma", Font.BOLD, 70));
205 btn5.setBounds(120, 300, 100, 100);
206 frame.getContentPane().add(btn5);
207
208 JButton btn6 = new JButton("6");
209 btn6.addActionListener(new ActionListener() {
210 public void actionPerformed(ActionEvent arg0) {
211
212 String EnterValue = txtDisplay.getText() + btn6.getText();
213 txtDisplay.setText(EnterValue);
214
215 }
216 });
217 btn6.setFont(new Font("Tahoma", Font.BOLD, 70));
218 btn6.setBounds(230, 300, 100, 100);
219 frame.getContentPane().add(btn6);
220
221 JButton btnx = new JButton("X");
222 btnx.addActionListener(new ActionListener() {
223 public void actionPerformed(ActionEvent arg0) {
224
225 firstnum = Double.parseDouble(txtDisplay.getText());
226 txtDisplay.setText("");
227 operations = "X";
228
229 }
230 });
231 btnx.setFont(new Font("Tahoma", Font.BOLD, 70));
232 btnx.setBounds(340, 300, 100, 100);
233 frame.getContentPane().add(btnx);
234
235 //===================ROW 4=============================
236

Page 4
DemoCalculator.java

237 JButton btn1 = new JButton("1");


238 btn1.addActionListener(new ActionListener() {
239 public void actionPerformed(ActionEvent arg0) {
240
241 String EnterValue = txtDisplay.getText() + btn1.getText();
242 txtDisplay.setText(EnterValue);
243
244 }
245 });
246 btn1.setFont(new Font("Tahoma", Font.BOLD, 70));
247 btn1.setBounds(10, 410, 100, 100);
248 frame.getContentPane().add(btn1);
249
250 JButton btn2 = new JButton("2");
251 btn2.addActionListener(new ActionListener() {
252 public void actionPerformed(ActionEvent arg0) {
253
254 String EnterValue = txtDisplay.getText() + btn2.getText();
255 txtDisplay.setText(EnterValue);
256
257 }
258 });
259 btn2.setFont(new Font("Tahoma", Font.BOLD, 70));
260 btn2.setBounds(120, 410, 100, 100);
261 frame.getContentPane().add(btn2);
262
263 JButton btn3 = new JButton("3");
264 btn3.addActionListener(new ActionListener() {
265 public void actionPerformed(ActionEvent arg0) {
266
267 String EnterValue = txtDisplay.getText() + btn3.getText();
268 txtDisplay.setText(EnterValue);
269
270 }
271 });
272 btn3.setFont(new Font("Tahoma", Font.BOLD, 70));
273 btn3.setBounds(230, 410, 100, 100);
274 frame.getContentPane().add(btn3);
275
276 JButton btndivide = new JButton("/");
277 btndivide.addActionListener(new ActionListener() {
278 public void actionPerformed(ActionEvent arg0) {
279
280 firstnum = Double.parseDouble(txtDisplay.getText());
281 txtDisplay.setText("");
282 operations = "/";
283
284 }
285 });
286 btndivide.setFont(new Font("Tahoma", Font.BOLD, 70));
287 btndivide.setBounds(340, 410, 100, 100);
288 frame.getContentPane().add(btndivide);
289
290
291 //===================ROW 5=============================
292
293 JButton btn0 = new JButton("0");

Page 5
DemoCalculator.java

294 btn0.addActionListener(new ActionListener() {


295 public void actionPerformed(ActionEvent arg0) {
296
297 String EnterValue = txtDisplay.getText() + btn0.getText();
298 txtDisplay.setText(EnterValue);
299
300 }
301 });
302 btn0.setFont(new Font("Tahoma", Font.BOLD, 70));
303 btn0.setBounds(10, 520, 100, 100);
304 frame.getContentPane().add(btn0);
305
306 JButton btnDot = new JButton(".");
307 btnDot.addActionListener(new ActionListener() {
308 public void actionPerformed(ActionEvent arg0) {
309
310 String EnterValue = txtDisplay.getText() + btnDot.getText();
311 txtDisplay.setText(EnterValue);
312
313 }
314 });
315 btnDot.setFont(new Font("Tahoma", Font.BOLD, 70));
316 btnDot.setBounds(120, 520, 100, 100);
317 frame.getContentPane().add(btnDot);
318
319 JButton btnPM = new JButton("\u00B1");
320 btnPM.addActionListener(new ActionListener() {
321 public void actionPerformed(ActionEvent arg0) {
322
323 double plusminus =
Double.parseDouble(String.valueOf(txtDisplay.getText()));
324 plusminus = plusminus*(-1);
325 txtDisplay.setText(String.valueOf(plusminus));
326
327 }
328 });
329 btnPM.setFont(new Font("Tahoma", Font.BOLD, 70));
330 btnPM.setBounds(230, 520, 100, 100);
331 frame.getContentPane().add(btnPM);
332
333 JButton btnequal = new JButton("=");
334 btnequal.addActionListener(new ActionListener() {
335 public void actionPerformed(ActionEvent arg0) {
336
337 String answer;
338
339 secondnum = Double.parseDouble(txtDisplay.getText());
340
341 if(operations == "+")
342 {
343 result = firstnum + secondnum;
344 answer = String.format("%.2f", result);
345 txtDisplay.setText(answer);
346 }
347 else if(operations == "-")
348 {
349 result = firstnum - secondnum;

Page 6
DemoCalculator.java

350 answer = String.format("%.2f", result);


351 txtDisplay.setText(answer);
352 }
353 else if(operations == "X")
354 {
355 result = firstnum * secondnum;
356 answer = String.format("%.2f", result);
357 txtDisplay.setText(answer);
358 }
359 else if(operations == "/")
360 {
361 result = firstnum / secondnum;
362 answer = String.format("%.2f", result);
363 txtDisplay.setText(answer);
364 }
365 else if(operations == "%")
366 {
367 result = firstnum % secondnum;
368 answer = String.format("%.2f", result);
369 txtDisplay.setText(answer);
370 }
371
372 }
373 });
374 btnequal.setFont(new Font("Tahoma", Font.BOLD, 70));
375 btnequal.setBounds(340, 520, 100, 100);
376 frame.getContentPane().add(btnequal);
377 }
378
379 }
380

Page 7

You might also like