-
Notifications
You must be signed in to change notification settings - Fork 2
/
calculator.java
185 lines (172 loc) · 4.66 KB
/
calculator.java
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class sample extends JFrame implements ActionListener
{
JPanel panel;
JTextField textfield;
JButton[] numberButtons,functionButtons;
JButton addButton,subButton,mulButton,divButton;
JButton delButton,decButton,clrButton,negButton,equButton;
double num1,num2,result;
char operator;
Font myfont=new Font("SansSerif",Font.BOLD,25);
public sample()
{
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
this.setLayout(null);
this.setSize(450,550);
this.setResizable(false);
textfield=new JTextField();
textfield.setBounds(50,25,350,60);
textfield.setEditable(false);
textfield.setFont(myfont);
panel=new JPanel();
panel.setLayout(new GridLayout(4,4,10,10));
panel.setBounds(50,100,350,350);
//panel.setBackground(new Color(3,67,49));
addButton=new JButton("+");
subButton=new JButton("-");
mulButton=new JButton("*");
divButton=new JButton("/");
decButton=new JButton(".");
equButton=new JButton("=");
clrButton=new JButton("clr");
delButton=new JButton("del");
negButton=new JButton("(-)");
functionButtons=new JButton[9];
functionButtons[0]=addButton;
functionButtons[1]=subButton;
functionButtons[2]=mulButton;
functionButtons[3]=divButton;
functionButtons[4]=clrButton;
functionButtons[5]=decButton;
functionButtons[6]=delButton;
functionButtons[7]=equButton;
functionButtons[8]=negButton;
for(int i=0 ; i<9 ; i++)
{
functionButtons[i].addActionListener(this);
functionButtons[i].setFocusable(false);
functionButtons[i].setFont(myfont);
}
numberButtons=new JButton[10];
for(int i=0 ; i<10 ; i++)
{
numberButtons[i]=new JButton(String.valueOf(i));
numberButtons[i].addActionListener(this);
numberButtons[i].setFont(myfont);
numberButtons[i].setFocusable(false);
}
panel.add(numberButtons[1]);
panel.add(numberButtons[2]);
panel.add(numberButtons[3]);
panel.add(addButton);
panel.add(numberButtons[4]);
panel.add(numberButtons[5]);
panel.add(numberButtons[6]);
panel.add(subButton);
panel.add(numberButtons[7]);
panel.add(numberButtons[8]);
panel.add(numberButtons[9]);
panel.add(mulButton);
panel.add(decButton);
panel.add(numberButtons[0]);
panel.add(equButton);
panel.add(divButton);
clrButton.setBounds(60,460,110,50);
delButton.setBounds(170,460,110,50);
negButton.setBounds(280,460,110,50);
this.add(clrButton);
this.add(delButton);
this.add(negButton);
this.add(panel);
this.add(textfield);
this.setVisible(true);
}
public static void main(String[] args)
{
sample c=new sample();
}
public void actionPerformed(ActionEvent e)
{
for(int i=0 ; i<10 ; i++)
{
if(e.getSource()==numberButtons[i])
{
textfield.setText(textfield.getText().concat(String.valueOf(i)));
}
}
if(e.getSource()==decButton)
{
textfield.setText(textfield.getText()+".");
}
if(e.getSource()==addButton)
{
num1=Double.parseDouble(textfield.getText());
operator='+';
textfield.setText("");
}
if(e.getSource()==subButton)
{
num1=Double.parseDouble(textfield.getText());
operator='-';
textfield.setText("");
}
if(e.getSource()==mulButton)
{
num1=Double.parseDouble(textfield.getText());
operator='*';
textfield.setText("");
}
if(e.getSource()==divButton)
{
num1=Double.parseDouble(textfield.getText());
operator='/';
textfield.setText("");
}
if(e.getSource()==equButton)
{
num2=Double.parseDouble(textfield.getText());
switch(operator)
{
case '+':result=num1+num2;
break;
case '-':result=num1-num2;
break;
case '*':result=num1*num2;
break;
case '/':result=num1/num2;
break;
}
textfield.setText(String.valueOf(result));
num1=result;
}
if(e.getSource()==negButton)
{
double temp=Double.parseDouble(textfield.getText());
temp*=-1;
textfield.setText(String.valueOf(temp));
}
if(e.getSource()==clrButton)
{
textfield.setText("");
}
if(e.getSource()==delButton)
{
String str=textfield.getText();
textfield.setText("");
for(int i=0 ; i<str.length()-1 ; i++)
{
textfield.setText(textfield.getText()+str.charAt(i));
}
}
}
}