|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +class demo extends Frame implements ActionListener |
| 4 | +{ |
| 5 | + TextField t1; |
| 6 | + Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16; |
| 7 | + int ch; |
| 8 | + int a,b,c; |
| 9 | + demo() |
| 10 | + { |
| 11 | + setVisible(true); |
| 12 | + setSize(200,300); |
| 13 | + setLocation(200,300); |
| 14 | + //setDefaultCloseOperation(Frame.EXIT_ON_CLOSE); |
| 15 | + setLayout(null); |
| 16 | + setTitle("Simple Calculator"); |
| 17 | + |
| 18 | + t1=new TextField(10); |
| 19 | + t1.setBounds(100,50,450,50); |
| 20 | + b1=new Button("1"); |
| 21 | + |
| 22 | + b2=new Button("2"); |
| 23 | + |
| 24 | + b3=new Button("3"); |
| 25 | + |
| 26 | + b4=new Button("4"); |
| 27 | + |
| 28 | + b5=new Button("5"); |
| 29 | + |
| 30 | + b6=new Button("6"); |
| 31 | + |
| 32 | + b7=new Button("7"); |
| 33 | + |
| 34 | + b8=new Button("8"); |
| 35 | + |
| 36 | + b9=new Button("9"); |
| 37 | + |
| 38 | + b10=new Button("0"); |
| 39 | + |
| 40 | + b11=new Button("+"); |
| 41 | + |
| 42 | + b12=new Button("-"); |
| 43 | + |
| 44 | + b13=new Button("*"); |
| 45 | + |
| 46 | + b14=new Button("/"); |
| 47 | + |
| 48 | + b15=new Button("clear"); |
| 49 | + |
| 50 | + b16=new Button("="); |
| 51 | + Panel p=new Panel(); |
| 52 | + p.setBackground(Color.pink); |
| 53 | + p.setLayout(new GridLayout(4,4,9,9)); |
| 54 | + p.setBounds(100,120,450,400); |
| 55 | + add(t1); |
| 56 | + p.add(b7);p.add(b8);p.add(b9);p.add(b11); |
| 57 | + p.add(b4);p.add(b5);p.add(b6);p.add(b12); |
| 58 | + p.add(b1);p.add(b2);p.add(b3);p.add(b13); |
| 59 | + p.add(b15);p.add(b10);p.add(b16);p.add(b14); |
| 60 | + add(p); |
| 61 | + b1.addActionListener(this); |
| 62 | + b2.addActionListener(this); |
| 63 | + b3.addActionListener(this); |
| 64 | + b4.addActionListener(this); |
| 65 | + b5.addActionListener(this); |
| 66 | + b6.addActionListener(this); |
| 67 | + b7.addActionListener(this); |
| 68 | + b8.addActionListener(this); |
| 69 | + b9.addActionListener(this); |
| 70 | + b10.addActionListener(this); |
| 71 | + b11.addActionListener(this); |
| 72 | + b12.addActionListener(this); |
| 73 | + b13.addActionListener(this); |
| 74 | + b14.addActionListener(this); |
| 75 | + b15.addActionListener(this); |
| 76 | + b16.addActionListener(this); |
| 77 | + } |
| 78 | + public void actionPerformed(ActionEvent ae) |
| 79 | + { |
| 80 | + |
| 81 | + try |
| 82 | + { |
| 83 | + if(ae.getSource()==b1) |
| 84 | + { |
| 85 | + t1.setText(t1.getText()+"1"); |
| 86 | + } |
| 87 | + if(ae.getSource()==b2) |
| 88 | + { |
| 89 | + t1.setText(t1.getText()+"2"); |
| 90 | + } |
| 91 | + if(ae.getSource()==b3) |
| 92 | + { |
| 93 | + t1.setText(t1.getText()+"3"); |
| 94 | + } |
| 95 | + if(ae.getSource()==b4) |
| 96 | + { |
| 97 | + t1.setText(t1.getText()+"4"); |
| 98 | + } |
| 99 | + if(ae.getSource()==b5) |
| 100 | + { |
| 101 | + t1.setText(t1.getText()+"5"); |
| 102 | + } |
| 103 | + if(ae.getSource()==b6) |
| 104 | + { |
| 105 | + t1.setText(t1.getText()+"6"); |
| 106 | + } |
| 107 | + if(ae.getSource()==b7) |
| 108 | + { |
| 109 | + t1.setText(t1.getText()+"7"); |
| 110 | + } |
| 111 | + if(ae.getSource()==b8) |
| 112 | + { |
| 113 | + t1.setText(t1.getText()+"8"); |
| 114 | + } |
| 115 | + if(ae.getSource()==b9) |
| 116 | + { |
| 117 | + t1.setText(t1.getText()+"9"); |
| 118 | + } |
| 119 | + if(ae.getSource()==b10) |
| 120 | + { |
| 121 | + t1.setText(t1.getText()+"0"); |
| 122 | + } |
| 123 | + if(ae.getSource()==b11) |
| 124 | + { |
| 125 | + a=Integer.parseInt(t1.getText()); |
| 126 | + ch=1; |
| 127 | + t1.setText(""); |
| 128 | + } |
| 129 | + if(ae.getSource()==b12) |
| 130 | + { |
| 131 | + a=Integer.parseInt(t1.getText()); |
| 132 | + ch=2; |
| 133 | + t1.setText(""); |
| 134 | + } |
| 135 | + if(ae.getSource()==b13) |
| 136 | + { |
| 137 | + a=Integer.parseInt(t1.getText()); |
| 138 | + ch=3; |
| 139 | + t1.setText(""); |
| 140 | + } |
| 141 | + if(ae.getSource()==b14) |
| 142 | + { |
| 143 | + a=Integer.parseInt(t1.getText()); |
| 144 | + ch=4; |
| 145 | + t1.setText(""); |
| 146 | + } |
| 147 | + if(ae.getSource()==b15) |
| 148 | + { |
| 149 | + t1.setText(" "); |
| 150 | + } |
| 151 | + if(ae.getSource()==b16) |
| 152 | + { |
| 153 | + b=Integer.parseInt(t1.getText()); |
| 154 | + if(ch==1) |
| 155 | + { |
| 156 | + c=a+b; |
| 157 | + t1.setText(""+a+"+"+b+"="+c); |
| 158 | + } |
| 159 | + if(ch==2) |
| 160 | + { |
| 161 | + c=a-b; |
| 162 | + t1.setText(""+a+"-"+b+"="+c); |
| 163 | + } |
| 164 | + if(ch==3) |
| 165 | + { |
| 166 | + c=a*b; |
| 167 | + t1.setText(""+a+"*"+b+"="+c); |
| 168 | + } |
| 169 | + if(ch==4) |
| 170 | + { |
| 171 | + c=a/b; |
| 172 | + t1.setText(""+a+"/"+b+"="+c); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + catch(Exception e) |
| 177 | + { |
| 178 | + t1.setText("Syntax Error ......!"); |
| 179 | + } |
| 180 | + |
| 181 | + } |
| 182 | + public static void main(String ar[]) |
| 183 | + { |
| 184 | + new demo(); |
| 185 | + } |
| 186 | +} |
| 187 | + |
0 commit comments