|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.ActionEvent; |
| 3 | +import java.awt.event.ActionListener; |
| 4 | +import java.awt.event.KeyEvent; |
| 5 | +import java.awt.event.KeyListener; |
| 6 | +import java.awt.event.WindowEvent; |
| 7 | +import java.awt.event.WindowListener; |
| 8 | +public class Calculator_frame extends Frame implements ActionListener |
| 9 | +{ |
| 10 | + TextField tf; |
| 11 | + Label l1,l2; |
| 12 | + Button b1, b2, b3, b4, b5, b6, b7,b8, b9, b0, bA, bM, c, eq, bD, bS; |
| 13 | + String s = ""; |
| 14 | + |
| 15 | + public Calculator_frame() { |
| 16 | + l1=new Label("Calculator X"); |
| 17 | + l1.setBounds(100,30,100,30); |
| 18 | + l1.setFont(new Font("Verdana", Font.BOLD, 15)); |
| 19 | + tf = new TextField(); |
| 20 | + setSize(300,350); |
| 21 | + |
| 22 | + tf.setBounds(10, 60, 200, 30); |
| 23 | + tf.setText("0"); |
| 24 | + tf.setEditable(false); |
| 25 | + |
| 26 | + b9 = new Button("9"); |
| 27 | + b9.setBounds(10,90, 40, 40); |
| 28 | + b9.setActionCommand("b9"); |
| 29 | + b9.addActionListener(this); |
| 30 | + b9.setBackground(Color.LIGHT_GRAY); |
| 31 | + |
| 32 | + b8 = new Button("8"); |
| 33 | + b8.setBounds(50,90, 40, 40); |
| 34 | + b8.setActionCommand("b8"); |
| 35 | + b8.addActionListener(this); |
| 36 | + |
| 37 | + b7 = new Button("7"); |
| 38 | + b7.setBounds(90,90, 40, 40); |
| 39 | + b7.setActionCommand("b7"); |
| 40 | + b7.addActionListener(this); |
| 41 | + |
| 42 | + b6 = new Button("6"); |
| 43 | + b6.setBounds(10,130, 40, 40); |
| 44 | + b6.setActionCommand("b6"); |
| 45 | + b6.addActionListener(this); |
| 46 | + |
| 47 | + b5 = new Button("5"); |
| 48 | + b5.setBounds(50,130, 40, 40); |
| 49 | + b5.setActionCommand("b5"); |
| 50 | + b5.addActionListener(this); |
| 51 | + |
| 52 | + b4 = new Button("4"); |
| 53 | + b4.setBounds(90,130, 40, 40); |
| 54 | + b4.setActionCommand("b4"); |
| 55 | + b4.addActionListener(this); |
| 56 | + |
| 57 | + b3 = new Button("3"); |
| 58 | + b3.setBounds(10,170, 40, 40); |
| 59 | + b3.setActionCommand("b3"); |
| 60 | + b3.addActionListener(this); |
| 61 | + |
| 62 | + b2 = new Button("2"); |
| 63 | + b2.setBounds(50,170, 40, 40); |
| 64 | + b2.setActionCommand("b2"); |
| 65 | + b2.addActionListener(this); |
| 66 | + |
| 67 | + b1 = new Button("1"); |
| 68 | + b1.setBounds(90,170, 40, 40); |
| 69 | + b1.setActionCommand("b1"); |
| 70 | + b1.addActionListener(this); |
| 71 | + |
| 72 | + b0 = new Button("0"); |
| 73 | + b0.setBounds(10,210, 40, 40); |
| 74 | + b0.setActionCommand("b0"); |
| 75 | + b0.addActionListener(this); |
| 76 | + |
| 77 | + eq = new Button("="); |
| 78 | + eq.setBounds(50,210, 80, 40); |
| 79 | + eq.setActionCommand("eq"); |
| 80 | + eq.addActionListener(this); |
| 81 | + |
| 82 | + c = new Button("C"); |
| 83 | + c.setBounds(130,90, 80, 40); |
| 84 | + c.setActionCommand("c"); |
| 85 | + c.addActionListener(this); |
| 86 | + |
| 87 | + bA = new Button("+"); |
| 88 | + bA.setBounds(130,130, 40, 40); |
| 89 | + bA.setActionCommand("bA"); |
| 90 | + bA.addActionListener(this); |
| 91 | + |
| 92 | + bS = new Button("-"); |
| 93 | + bS.setBounds(170,130, 40, 40); |
| 94 | + bS.setActionCommand("bS"); |
| 95 | + bS.addActionListener(this); |
| 96 | + |
| 97 | + bM = new Button("X"); |
| 98 | + bM.setBounds(130,170, 40, 40); |
| 99 | + bM.setActionCommand("bM"); |
| 100 | + bM.addActionListener(this); |
| 101 | + |
| 102 | + bD = new Button("/"); |
| 103 | + bD.setBounds(170,170, 40, 40); |
| 104 | + bD.setActionCommand("bD"); |
| 105 | + bD.addActionListener(this); |
| 106 | + l2=new Label("Made By: Rajesh"); |
| 107 | + l2.setBounds(10,250,250,40); |
| 108 | + add(l1);add(tf);add(b9);add(b8);add(b7);add(b6);add(b5);add(b4);add(b3);add(b2);add(b1);add(b0);add(eq); |
| 109 | + add(c);add(bA);add(bS);add(bM);add(bD);add(l2); |
| 110 | + |
| 111 | + addWindowListener(new WindowListener() { |
| 112 | + public void windowOpened(WindowEvent e) {} |
| 113 | + public void windowIconified(WindowEvent e) {} |
| 114 | + public void windowDeiconified(WindowEvent e) {} |
| 115 | + public void windowDeactivated(WindowEvent e) {} |
| 116 | + public void windowClosing(WindowEvent e) { dispose();} |
| 117 | + public void windowClosed(WindowEvent e) {} |
| 118 | + public void windowActivated(WindowEvent e) {} |
| 119 | + }); |
| 120 | + |
| 121 | + addKeyListener(new KeyListener() { |
| 122 | + public void keyTyped(KeyEvent e) { |
| 123 | + char c=e.getKeyChar(); |
| 124 | + switch(c) |
| 125 | + { |
| 126 | + case '1': s=s.concat("1");tf.setText(s); |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + public void keyReleased(KeyEvent e) {} |
| 131 | + public void keyPressed(KeyEvent e) {} |
| 132 | + |
| 133 | + }); |
| 134 | + setLayout(null); |
| 135 | + setVisible(true); |
| 136 | + |
| 137 | + } |
| 138 | + public int calcString(String a){ |
| 139 | + String operators[]=a.split("[0-9]+"); |
| 140 | + String operands[]=a.split("\\+|\\-|x|\\/",0); |
| 141 | + |
| 142 | + /*StringBuffer sb = new StringBuffer(); |
| 143 | + for(int i = 0; i < operands.length; i++) { |
| 144 | + sb.append(operands[i]); |
| 145 | + } |
| 146 | + String str = sb.toString(); |
| 147 | + System.out.println(str);*/ |
| 148 | + |
| 149 | + int agregate = Integer.parseInt(operands[0]); |
| 150 | + for(int i=1;i<operands.length;i++){ |
| 151 | + if(operators[i].equals("+")) |
| 152 | + agregate += Integer.parseInt(operands[i]); |
| 153 | + if(operators[i].equals("-")) |
| 154 | + agregate -= Integer.parseInt(operands[i]); |
| 155 | + if(operators[i].equals("x")) |
| 156 | + agregate *= Integer.parseInt(operands[i]); |
| 157 | + if(operators[i].equals("/")) |
| 158 | + agregate /= Integer.parseInt(operands[i]); |
| 159 | + } |
| 160 | + return agregate; |
| 161 | + } |
| 162 | + |
| 163 | + public void actionPerformed(ActionEvent e) |
| 164 | + { |
| 165 | + String c=e.getActionCommand(); |
| 166 | + switch(c) |
| 167 | + { |
| 168 | + case "b0" :s=s.concat("0");tf.setText(s); |
| 169 | + break; |
| 170 | + case "b1" :s=s.concat("1");tf.setText(s); |
| 171 | + break; |
| 172 | + case "b2" :s=s.concat("2");tf.setText(s); |
| 173 | + break; |
| 174 | + case "b3" : s+="3";tf.setText(s); |
| 175 | + break; |
| 176 | + case "b4" : s=s.concat("4");tf.setText(s); |
| 177 | + break; |
| 178 | + case "b5" : s=s.concat("5");tf.setText(s); |
| 179 | + break; |
| 180 | + case "b6" : s=s.concat("6");tf.setText(s); |
| 181 | + break; |
| 182 | + case "b7" : s=s.concat("7");tf.setText(s); |
| 183 | + break; |
| 184 | + case "b8" : s=s.concat("8");tf.setText(s); |
| 185 | + break; |
| 186 | + case "b9" : s=s.concat("9");tf.setText(s); |
| 187 | + break; |
| 188 | + case "c" : tf.setText("0");s=new String(""); |
| 189 | + break; |
| 190 | + case "eq" : s=Integer.toString(calcString(s));tf.setText(s);s=new String(""); |
| 191 | + break; |
| 192 | + case "bA" : s=s.concat("+");tf.setText(s); |
| 193 | + break; |
| 194 | + case "bS" : s=s.concat("-");tf.setText(s); |
| 195 | + break; |
| 196 | + case "bD" : s=s.concat("/");tf.setText(s); |
| 197 | + break; |
| 198 | + case "bM" : s=s.concat("x");tf.setText(s); |
| 199 | + } |
| 200 | + |
| 201 | + } |
| 202 | + public static void main(String[] args) |
| 203 | + { |
| 204 | + new Calculator_frame(); |
| 205 | + } |
| 206 | + |
| 207 | +} |
0 commit comments