Simple Java GUI Calculator that performs basic mathematical calculations (addition, subtraction, multiplication and divide). In addition, it can calculate the sine, cosine, tangent, log, square and square root of numbers.
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 |
/** *website: www.cscprogrammingtutorials.com */ import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.*; public class Calculator extends JFrame { JButton zero = new JButton(" 0 "); JButton one = new JButton(" 1 "); JButton two = new JButton(" 2 "); JButton three = new JButton(" 3 "); JButton four = new JButton(" 4 "); JButton five = new JButton(" 5 "); JButton six = new JButton(" 6 "); JButton seven = new JButton(" 7 "); JButton eight = new JButton(" 8 "); JButton nine = new JButton(" 9 "); JButton plus = new JButton(" + "); JButton minus = new JButton(" - "); JButton times = new JButton(" * "); JButton divide = new JButton(" / "); JButton square = new JButton(" ^2 "); JButton squareroot = new JButton(" 2/x "); JButton log = new JButton(" log "); JButton degrees = new JButton(" DEG "); JButton radians = new JButton(" RAD "); JButton pi = new JButton(" pi "); JButton equals = new JButton(" = "); JButton sin = new JButton(" sin "); JButton cos = new JButton(" cos "); JButton tan = new JButton(" tan "); JButton point = new JButton(" . "); JButton clear = new JButton(" Clear "); String value; char operator; JTextArea area = new JTextArea(3, 5); public static void main(String[] args) { Calculator calculator = new Calculator(); calculator.setSize(230, 340); calculator.setTitle(" Calculator "); calculator.setResizable(false); calculator.setVisible(true); calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public Calculator() { add(new JScrollPane(area), BorderLayout.NORTH); JPanel buttonpanel = new JPanel(); buttonpanel.setLayout(new FlowLayout()); buttonpanel.add(degrees); buttonpanel.add(radians); buttonpanel.add(pi); buttonpanel.add(sin); buttonpanel.add(cos); buttonpanel.add(tan); buttonpanel.add(square); buttonpanel.add(log); buttonpanel.add(squareroot); buttonpanel.add(seven); buttonpanel.add(eight); buttonpanel.add(nine); buttonpanel.add(divide); buttonpanel.add(four); buttonpanel.add(five); buttonpanel.add(six); buttonpanel.add(times); buttonpanel.add(one); buttonpanel.add(two); buttonpanel.add(three); buttonpanel.add(minus); buttonpanel.add(zero); buttonpanel.add(point); buttonpanel.add(plus); buttonpanel.add(clear); buttonpanel.add(equals); add(buttonpanel, BorderLayout.CENTER); area.setForeground(Color.BLACK); area.setBackground(Color.WHITE); area.setLineWrap(true); area.setWrapStyleWord(true); area.setEditable(false); zero.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("0".toString()); } }); one.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("1".toString()); } }); two.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("2".toString()); } }); three.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("3".toString()); } }); four.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("4".toString()); } }); five.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("5".toString()); } }); six.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("6".toString()); } }); seven.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("7".toString()); } }); eight.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("8".toString()); } }); nine.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("9".toString()); } }); plus.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("+".toString()); operator = '+'; } }); minus.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("-".toString()); operator = '-'; } }); times.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("*".toString()); operator = '*'; } }); divide.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("/".toString()); operator = '/'; } }); square.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("^2".toString()); } }); squareroot.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("Squareroot".toString()); value = "Squareroot"; } }); log.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("log".toString()); value = "log"; } }); degrees.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("deg".toString()); value = "deg"; } }); radians.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("rad".toString()); value = "rad"; } }); pi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("pi".toString()); value = "pi"; } }); equals.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { String leftSide String rightSide; try { leftSide = area.getText().substring(0, area.getText().indexOf(operator + "")); rightSide = area.getText().substring(area.getText().indexOf(operator + "") + 1, area.getText().length()); switch (operator) { case '+': area.append(" = " + ((Double.parseDouble(leftSide) + Double.parseDouble(rightSide)))); break; case '-': area.append(" = " + ((Double.parseDouble(leftSide) - Double.parseDouble(rightSide)))); break; case '/': area.append(" = " + ((Double.parseDouble(leftSide) / Double.parseDouble(rightSide)))); break; case '*': area.append(" = " + ((Double.parseDouble(leftSide) * Double.parseDouble(rightSide)))); break; default: area.setText(" No Result... "); break; } } catch (Exception e) { } try { String sinValue; sinValue = area.getText().substring(area.getText().indexOf(value) + 1, area.getText().length()); area.append("=" + Math.sin((Double.parseDouble(sinValue)))); } catch (Exception e) { } String cosValue; try { cosValue = area.getText().substring(area.getText().indexOf("cos") + 1, area.getText().length()); area.append("=" + Math.cos((Double.parseDouble(cosValue)))); } catch (Exception e) { } String tanValue; try { tanValue = area.getText().substring(area.getText().indexOf("tan") + 1, area.getText().length()); area.append("=" + Math.tan((Double.parseDouble(tanValue)))); } catch (Exception e) { } } }); sin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { value = "sin"; } }); cos.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("cos".toString()); } }); tan.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append("tan".toString()); } }); point.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.append(".".toString()); } }); clear.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent f) { area.setText(""); } }); } } |
Below is a screenshot of the output when the program is run.
1 Comment
This program is working and its very simple thanks for sharing this Calculator program code. It help me a lot in my college exams.