|
| 1 | +import javax.swing.*; |
| 2 | +import java.awt.*; |
| 3 | +import java.awt.event.*; |
| 4 | +import javax.swing.BorderFactory; |
| 5 | +import javax.swing.border.Border; |
| 6 | +public class calculator implements ActionListener |
| 7 | +{ |
| 8 | +JFrame frame = new JFrame(); |
| 9 | +JPanel panel = new JPanel(); |
| 10 | +JTextArea textarea = new JTextArea(2,10); |
| 11 | +JButton b1 = new JButton("1"); |
| 12 | +JButton b2 = new JButton("2"); |
| 13 | +JButton b3 = new JButton("3"); |
| 14 | +JButton b4 = new JButton("4"); |
| 15 | +JButton b5 = new JButton("5"); |
| 16 | +JButton b6 = new JButton("6"); |
| 17 | +JButton b7 = new JButton("7"); |
| 18 | +JButton b8 = new JButton("8"); |
| 19 | +JButton b9 = new JButton("9"); |
| 20 | +JButton b0 = new JButton("0"); |
| 21 | +JButton badd = new JButton("+"); |
| 22 | +JButton bsub = new JButton("-"); |
| 23 | +JButton bmul = new JButton("*"); |
| 24 | +JButton bdiv = new JButton("/"); |
| 25 | +JButton bequal = new JButton("="); |
| 26 | +JButton bclear = new JButton("C"); |
| 27 | +JButton bdot = new JButton("."); |
| 28 | +double n1 , n2 , result ; |
| 29 | +int addc = 0; |
| 30 | +int subc =0; |
| 31 | +int mulc = 0; |
| 32 | +int divc =0; |
| 33 | +public calculator() |
| 34 | +{ |
| 35 | +frame.setSize(340,450); |
| 36 | +frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 37 | +frame.setVisible(true); |
| 38 | +frame.setTitle("FIRST PROJECT"); |
| 39 | +frame.add(panel); |
| 40 | +frame.setBackground(Color.BLUE); |
| 41 | +Border border = BorderFactory.createLineBorder(Color.BLACK); |
| 42 | +panel.add(textarea); |
| 43 | +textarea.setBackground(Color.WHITE); |
| 44 | +textarea.setBorder(border); |
| 45 | +Font f = new Font("arial",Font.BOLD,33); |
| 46 | +textarea.setFont(f); |
| 47 | +textarea.setBackground(Color.WHITE); |
| 48 | +textarea.setForeground(Color.BLACK); |
| 49 | +textarea.setPreferredSize(new Dimension(2,10)); |
| 50 | +textarea.setLineWrap(true); |
| 51 | +b7.setPreferredSize(new Dimension(100,40)); |
| 52 | +b8.setPreferredSize(new Dimension(100,40)); |
| 53 | +b9.setPreferredSize(new Dimension(100,40)); |
| 54 | +b4.setPreferredSize(new Dimension(100,40)); |
| 55 | +b5.setPreferredSize(new Dimension(100,40)); |
| 56 | +b6.setPreferredSize(new Dimension(100,40)); |
| 57 | +b1.setPreferredSize(new Dimension(100,40)); |
| 58 | +b2.setPreferredSize(new Dimension(100,40)); |
| 59 | +b3.setPreferredSize(new Dimension(100,40)); |
| 60 | +b0.setPreferredSize(new Dimension(100,40)); |
| 61 | +badd.setPreferredSize(new Dimension(100,40)); |
| 62 | +bsub.setPreferredSize(new Dimension(100,40)); |
| 63 | +bmul.setPreferredSize(new Dimension(100,40)); |
| 64 | +bdiv.setPreferredSize(new Dimension(100,40)); |
| 65 | +bdot.setPreferredSize(new Dimension(150,40)); |
| 66 | +bclear.setPreferredSize(new Dimension(100,40)); |
| 67 | +bequal.setPreferredSize(new Dimension(150,40)); |
| 68 | +panel.setBackground(Color.PINK); |
| 69 | +panel.add(b7); |
| 70 | +panel.add(b8); |
| 71 | +panel.add(b9); |
| 72 | +panel.add(b4); |
| 73 | +panel.add(b5); |
| 74 | +panel.add(b6); |
| 75 | +panel.add(b1); |
| 76 | +panel.add(b2); |
| 77 | +panel.add(b3); |
| 78 | +panel.add(b0); |
| 79 | +panel.add(badd); |
| 80 | +panel.add(bsub); |
| 81 | +panel.add(bmul); |
| 82 | +panel.add(bdiv); |
| 83 | +panel.add(bclear); |
| 84 | +panel.add(bequal); |
| 85 | +panel.add(bdot); |
| 86 | +b0.setBackground(Color.BLACK); |
| 87 | +b1.setBackground(Color.BLACK); |
| 88 | +b2.setBackground(Color.BLACK); |
| 89 | +b3.setBackground(Color.BLACK); |
| 90 | +b4.setBackground(Color.BLACK); |
| 91 | +b5.setBackground(Color.BLACK); |
| 92 | +b6.setBackground(Color.BLACK); |
| 93 | +b7.setBackground(Color.BLACK); |
| 94 | +b8.setBackground(Color.BLACK); |
| 95 | +b9.setBackground(Color.BLACK); |
| 96 | +badd.setBackground(Color.BLACK); |
| 97 | +bsub.setBackground(Color.BLACK); |
| 98 | +bmul.setBackground(Color.BLACK); |
| 99 | +bdiv.setBackground(Color.BLACK); |
| 100 | +bequal.setBackground(Color.BLACK); |
| 101 | +bdot.setBackground(Color.BLACK); |
| 102 | +bclear.setBackground(Color.BLACK); |
| 103 | +b0.setForeground(Color.WHITE); |
| 104 | +b1.setForeground(Color.WHITE); |
| 105 | +b2.setForeground(Color.WHITE); |
| 106 | +b3.setForeground(Color.WHITE); |
| 107 | +b4.setForeground(Color.WHITE); |
| 108 | +b5.setForeground(Color.WHITE); |
| 109 | +b6.setForeground(Color.WHITE); |
| 110 | +b7.setForeground(Color.WHITE); |
| 111 | +b8.setForeground(Color.WHITE); |
| 112 | +b9.setForeground(Color.WHITE); |
| 113 | +bequal.setForeground(Color.WHITE); |
| 114 | +badd.setForeground(Color.WHITE); |
| 115 | +bsub.setForeground(Color.WHITE); |
| 116 | +bdiv.setForeground(Color.WHITE); |
| 117 | +bmul.setForeground(Color.WHITE); |
| 118 | +bclear.setForeground(Color.WHITE); |
| 119 | +bdot.setForeground(Color.WHITE); |
| 120 | +b1.addActionListener((ActionListener)this); |
| 121 | +b0.addActionListener((ActionListener)this); |
| 122 | +b2.addActionListener((ActionListener)this); |
| 123 | +b3.addActionListener((ActionListener)this); |
| 124 | +b4.addActionListener((ActionListener)this); |
| 125 | +b5.addActionListener((ActionListener)this); |
| 126 | +b6.addActionListener((ActionListener)this); |
| 127 | +b7.addActionListener((ActionListener)this); |
| 128 | +b8.addActionListener((ActionListener)this); |
| 129 | +b9.addActionListener((ActionListener)this); |
| 130 | +badd.addActionListener((ActionListener)this); |
| 131 | +bsub.addActionListener((ActionListener)this); |
| 132 | +bmul.addActionListener((ActionListener)this); |
| 133 | +bdiv.addActionListener((ActionListener)this); |
| 134 | +bclear.addActionListener((ActionListener)this); |
| 135 | +bequal.addActionListener((ActionListener)this); |
| 136 | +bdot.addActionListener((ActionListener)this); |
| 137 | +} |
| 138 | +public void actionPerformed(ActionEvent e) |
| 139 | +{ |
| 140 | +if(e.getSource()==bclear) |
| 141 | +{ |
| 142 | +n1 = 0.0; |
| 143 | +n2 = 0.0; |
| 144 | +textarea.setText(""); |
| 145 | +} |
| 146 | +if(e.getSource()==b1) |
| 147 | +{ |
| 148 | +textarea.append("1"); |
| 149 | +} |
| 150 | +if(e.getSource()==b2) |
| 151 | +{ |
| 152 | +textarea.append("2"); |
| 153 | +} |
| 154 | +if(e.getSource()==b0) |
| 155 | +{ |
| 156 | +textarea.append("0"); |
| 157 | +} |
| 158 | +if(e.getSource()==b3) |
| 159 | +{ |
| 160 | +textarea.append("3"); |
| 161 | +} |
| 162 | +if(e.getSource()==b4) |
| 163 | +{ |
| 164 | +textarea.append("4"); |
| 165 | +} |
| 166 | +if(e.getSource()==b5) |
| 167 | +{ |
| 168 | +textarea.append("5"); |
| 169 | +} |
| 170 | +if(e.getSource()==b6) |
| 171 | +{ |
| 172 | +textarea.append("6"); |
| 173 | +} |
| 174 | +if(e.getSource()==b7) |
| 175 | +{ |
| 176 | +textarea.append("7"); |
| 177 | +} |
| 178 | +if(e.getSource()==b8) |
| 179 | +{ |
| 180 | +textarea.append("8"); |
| 181 | +} |
| 182 | +if(e.getSource()==b9) |
| 183 | +{ |
| 184 | +textarea.append("9"); |
| 185 | +} |
| 186 | +if(e.getSource()==bdot) |
| 187 | +{ |
| 188 | + |
| 189 | +textarea.append("."); |
| 190 | +} |
| 191 | +if(e.getSource()==badd) |
| 192 | +{ |
| 193 | +n1 = number(); |
| 194 | +textarea.setText(""); |
| 195 | +addc = 1; |
| 196 | +subc = 0; |
| 197 | +mulc = 0; |
| 198 | +divc = 0; |
| 199 | +} |
| 200 | +if(e.getSource()==bsub) |
| 201 | +{ |
| 202 | +n1 = number(); |
| 203 | +textarea.setText(""); |
| 204 | +addc = 0; |
| 205 | +subc = 1; |
| 206 | +mulc = 0; |
| 207 | +divc = 0; |
| 208 | +} |
| 209 | +if(e.getSource()==bmul) |
| 210 | +{ |
| 211 | +n1 = number(); |
| 212 | +textarea.setText(""); |
| 213 | +addc = 0; |
| 214 | +subc = 0; |
| 215 | +mulc = 1; |
| 216 | +divc = 0; |
| 217 | +} |
| 218 | +if(e.getSource()==bdiv) |
| 219 | +{ |
| 220 | +n1 = number(); |
| 221 | +textarea.setText(""); |
| 222 | +addc = 0; |
| 223 | +subc = 0; |
| 224 | +mulc = 0; |
| 225 | +divc = 1; |
| 226 | +} |
| 227 | + |
| 228 | + |
| 229 | +if(e.getSource()==bequal) |
| 230 | +{ |
| 231 | +n2 = number(); |
| 232 | +if(addc > 0) |
| 233 | +{ |
| 234 | +result = n1+n2; |
| 235 | +textarea.setText(Double.toString(result)); |
| 236 | +} |
| 237 | +if(subc > 0) |
| 238 | +{ |
| 239 | +result = n1-n2; |
| 240 | +textarea.setText(Double.toString(result)); |
| 241 | +} |
| 242 | +if(mulc > 0) |
| 243 | +{ |
| 244 | +result = n1*n2; |
| 245 | +textarea.setText(Double.toString(result)); |
| 246 | +} |
| 247 | +if(divc > 0) |
| 248 | +{ |
| 249 | +result = n1/n2; |
| 250 | +textarea.setText(Double.toString(result)); |
| 251 | +} |
| 252 | +} |
| 253 | +} |
| 254 | + |
| 255 | +public double number() |
| 256 | +{ |
| 257 | +double n1 = 0.0; |
| 258 | +String s; |
| 259 | +s = textarea.getText(); |
| 260 | +n1 = Double.valueOf(s); |
| 261 | +return n1; |
| 262 | +} |
| 263 | +} |
0 commit comments