You are on page 1of 3

first.cpp http://localhost:4649/?

mode=clike

1 import javax.swing.*;
2 import java.awt.event.*;
3
4
5 class Calc implements ActionListener
6 {
7 JFrame f;
8 JTextField t;
9 JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdiv,bmul,bsub,badd,bdec,beq,bdel,bclr;
10
11 static double a=0,b=0,result=0;
12 static int operator=0;
13
14 Calc()
15 {
16 f=new JFrame("Calculator");
17 t=new JTextField();
18 b1=new JButton("1");
19 b2=new JButton("2");
20 b3=new JButton("3");
21 b4=new JButton("4");
22 b5=new JButton("5");
23 b6=new JButton("6");
24 b7=new JButton("7");
25 b8=new JButton("8");
26 b9=new JButton("9");
27 b0=new JButton("0");
28 bdiv=new JButton("/");
29 bmul=new JButton("*");
30 bsub=new JButton("-");
31 badd=new JButton("+");
32 bdec=new JButton(".");
33 beq=new JButton("=");
34 bdel=new JButton("Delete");
35 bclr=new JButton("Clear");
36 t.setBounds(30,40,280,30);
37 b7.setBounds(40,100,50,40);
38 b8.setBounds(110,100,50,40);
39 b9.setBounds(180,100,50,40);
40 bdiv.setBounds(250,100,50,40);
41 b4.setBounds(40,170,50,40);
42 b5.setBounds(110,170,50,40);
43 b6.setBounds(180,170,50,40);
44 bmul.setBounds(250,170,50,40);
45 b1.setBounds(40,240,50,40);
46 b2.setBounds(110,240,50,40);
47 b3.setBounds(180,240,50,40);
48 bsub.setBounds(250,240,50,40);
49 bdec.setBounds(40,310,50,40);
50 b0.setBounds(110,310,50,40);
51 beq.setBounds(180,310,50,40);
52 badd.setBounds(250,310,50,40);
53 bdel.setBounds(60,380,100,40);
54 bclr.setBounds(180,380,100,40);
55 f.add(t);
56 f.add(b7);
57 f.add(b8);
58 f.add(b9);
59 f.add(bdiv);
60 f.add(b4);

1 of 3 24-09-2021, 14:40
first.cpp http://localhost:4649/?mode=clike

61 f.add(b5);
62 f.add(b6);
63 f.add(bmul);
64 f.add(b1);
65 f.add(b2);
66 f.add(b3);
67 f.add(bsub);
68 f.add(bdec);
69 f.add(b0);
70 f.add(beq);
71 f.add(badd);
72 f.add(bdel);
73 f.add(bclr);
74 f.setLayout(null);
75 f.setVisible(true);
76 f.setSize(350,500);
77 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
78 f.setResizable(false);
79 b1.addActionListener(this);
80 b2.addActionListener(this);
81 b3.addActionListener(this);
82 b4.addActionListener(this);
83 b5.addActionListener(this);
84 b6.addActionListener(this);
85 b7.addActionListener(this);
86 b8.addActionListener(this);
87 b9.addActionListener(this);
88 b0.addActionListener(this);
89 badd.addActionListener(this);
90 bdiv.addActionListener(this);
91 bmul.addActionListener(this);
92 bsub.addActionListener(this);
93 bdec.addActionListener(this);
94 beq.addActionListener(this);
95 bdel.addActionListener(this);
96 bclr.addActionListener(this);
97 }
98
99 public void actionPerformed(ActionEvent e)
100 {
101 if(e.getSource()==b1)
102 t.setText(t.getText().concat("1"));
103 if(e.getSource()==b2)
104 t.setText(t.getText().concat("2"));
105 if(e.getSource()==b3)
106 t.setText(t.getText().concat("3"));
107 if(e.getSource()==b4)
108 t.setText(t.getText().concat("4"));
109 if(e.getSource()==b5)
110 t.setText(t.getText().concat("5"));
111 if(e.getSource()==b6)
112 t.setText(t.getText().concat("6"));
113 if(e.getSource()==b7)
114 t.setText(t.getText().concat("7"));
115 if(e.getSource()==b8)
116 t.setText(t.getText().concat("8"));
117 if(e.getSource()==b9)
118 t.setText(t.getText().concat("9"));
119 if(e.getSource()==b0)
120 t.setText(t.getText().concat("0"));

2 of 3 24-09-2021, 14:40
first.cpp http://localhost:4649/?mode=clike

121 if(e.getSource()==bdec)
122 t.setText(t.getText().concat("."));
123 if(e.getSource()==badd)
124 {
125 a=Double.parseDouble(t.getText());
126 operator=1;
127 t.setText("");
128 }
129 if(e.getSource()==bsub)
130 {
131 a=Double.parseDouble(t.getText());
132 operator=2;
133 t.setText("");
134 }
135 if(e.getSource()==bmul)
136 {
137 a=Double.parseDouble(t.getText());
138 operator=3;
139 t.setText("");
140 }
141 if(e.getSource()==bdiv)
142 {
143 a=Double.parseDouble(t.getText());
144 operator=4;
145 t.setText("");
146 }
147 if(e.getSource()==beq)
148 {
149 b=Double.parseDouble(t.getText());
150 switch(operator)
151 {
152 case 1: result=a+b;
153 break;
154 case 2: result=a-b;
155 break;
156 case 3: result=a*b;
157 break;
158 case 4: result=a/b;
159 break;
160 default: result=0;
161 }
162 t.setText(""+result);
163 }
164 if(e.getSource()==bclr)
165 t.setText("");
166 if(e.getSource()==bdel)
167 {
168 String s=t.getText();
169 t.setText("");
170 for(int i=0;i<s.length()-1;i++)
171 t.setText(t.getText()+s.charAt(i));
172 }
173 }
174
175 public static void main(String...s)
176 {
177 new Calc();
178 }
179 }

3 of 3 24-09-2021, 14:40

You might also like