-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.java
207 lines (163 loc) · 7.85 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* @author PashalisTsirts
* @create 5/28/2020, 8:37:06
* @name Calculator.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener
{
private JTextField Result;
private String FirstCharacter="",SecondCharacter="",ThirdCharacter="",GetCharacter="";
private static final long serialVersionUID=1L;
public static void main (String [] args)
{
new Calculator().setVisible(true);
}
private Calculator()
{
//Window settings
super("Calculator");
setBackground(Color.gray);
setSize(280,200);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout() );
// Creating and initialising each button and text field
JPanel buttonpanel1=new JPanel(); //Creating first panel
buttonpanel1.setLayout(new FlowLayout());
buttonpanel1.add(Result = new JTextField(15));
Result.setHorizontalAlignment(JTextField.RIGHT); //Place result to right
Result.setEditable(false); //Not editable text
JButton AC=new JButton("C"); //Creating button clear
AC.addActionListener(this);
AC.setActionCommand("C");
buttonpanel1.add(AC);
JPanel buttonpanel2=new JPanel(); //Creating second panel
buttonpanel2.setLayout(new GridLayout(4,4)); //Creating gridlayout
JButton b7 =new JButton ("7"); //Creating button 7
b7.addActionListener(this);
b7.setActionCommand("7");
buttonpanel2.add(b7);
JButton b8 =new JButton ("8"); //Creating button 8
b8.addActionListener(this);
b8.setActionCommand("8");
buttonpanel2.add(b8);
JButton b9 =new JButton ("9"); //Creating button 9
b9.addActionListener(this);
b9.setActionCommand("9");
buttonpanel2.add(b9);
JButton bd =new JButton ("/"); //Creating button slash
bd.addActionListener(this);
bd.setActionCommand("/");
buttonpanel2.add(bd);
JButton b4 =new JButton ("4"); //Creating button 4
b4.addActionListener(this);
b4.setActionCommand("4");
buttonpanel2.add(b4);
JButton b5 =new JButton ("5"); //Creating button 5
b5.addActionListener(this);
b5.setActionCommand("5");
buttonpanel2.add(b5);
JButton b6 =new JButton ("6"); //Creating button 6
b6.addActionListener(this);
b6.setActionCommand("6");
buttonpanel2.add(b6);
JButton bml =new JButton ("*"); //Creating button for multiplication
bml.addActionListener(this);
bml.setActionCommand("*");
buttonpanel2.add(bml);
JButton b1 =new JButton ("1"); //Creating button 1
b1.addActionListener(this);
b1.setActionCommand("1");
buttonpanel2.add(b1);
JButton b2 =new JButton ("2"); //Creating button 2
b2.addActionListener(this);
b2.setActionCommand("2");
buttonpanel2.add(b2);
JButton b3 =new JButton ("3"); //Creating button 3
b3.addActionListener(this);
b3.setActionCommand("3");
buttonpanel2.add(b3);
JButton bm =new JButton ("-"); //Creating button for minus
bm.addActionListener(this);
bm.setActionCommand("-");
buttonpanel2.add(bm);
JButton b0 =new JButton ("0"); //Creating button 0
b0.addActionListener(this);
b0.setActionCommand("0");
buttonpanel2.add(b0);
JButton bp =new JButton ("."); //Creating button point
bp.addActionListener(this);
bp.setActionCommand(".");
buttonpanel2.add(bp);
JButton be =new JButton ("="); //Creating button equal
be.addActionListener(this);
be.setActionCommand("=");
buttonpanel2.add(be);
JButton bpl =new JButton ("+"); //Creating button plus
bpl.addActionListener(this);
bpl.setActionCommand("+");
buttonpanel2.add(bpl);
JPanel mainpanel = new JPanel(); //Creating the main panel
mainpanel.setLayout(new BorderLayout());
mainpanel.add(buttonpanel1, BorderLayout.NORTH);
mainpanel.add(buttonpanel2, BorderLayout.SOUTH); //Place smaller panels
add(mainpanel); //Add main panel
}
//actions
public void actionPerformed(ActionEvent e)
{
GetCharacter=e.getActionCommand(); //Keypressed character
if ((GetCharacter.charAt(0) <= '9' && GetCharacter.charAt(0) >= '0') || GetCharacter.charAt(0) == '.')//If number
{
if (!SecondCharacter.equals(""))
ThirdCharacter = ThirdCharacter + GetCharacter;
else
FirstCharacter = FirstCharacter + GetCharacter;
Result.setText(FirstCharacter + SecondCharacter + ThirdCharacter); //Print result
}
else if (GetCharacter.charAt(0) == '=') //Calculate
{
double temp;
if (SecondCharacter.equals("*"))
temp = (Double.parseDouble(FirstCharacter) * Double.parseDouble(ThirdCharacter));
else if (SecondCharacter.equals("/"))
temp = (Double.parseDouble(FirstCharacter) / Double.parseDouble(ThirdCharacter));
else if (SecondCharacter.equals("+"))
temp = (Double.parseDouble(FirstCharacter) + Double.parseDouble(ThirdCharacter));
else
temp = (Double.parseDouble(FirstCharacter) - Double.parseDouble(ThirdCharacter));
Result.setText(FirstCharacter + SecondCharacter + ThirdCharacter + "=" + temp); //Print result
FirstCharacter = Double.toString(temp); //Convert to string
SecondCharacter = ThirdCharacter = "";
}
else if (GetCharacter.charAt(0) == 'C') //If clear
{
FirstCharacter = SecondCharacter = ThirdCharacter = "";
Result.setText(FirstCharacter + SecondCharacter + ThirdCharacter); //Clear result
}
else //If operator + calculate
{
if (SecondCharacter.equals("") || ThirdCharacter.equals(""))
SecondCharacter = GetCharacter;
else
{
double temp;
if (SecondCharacter.equals("*"))
temp = (Double.parseDouble(FirstCharacter) * Double.parseDouble(ThirdCharacter));
else if (SecondCharacter.equals("/"))
temp = (Double.parseDouble(FirstCharacter) / Double.parseDouble(ThirdCharacter));
else if (SecondCharacter.equals("+"))
temp = (Double.parseDouble(FirstCharacter) + Double.parseDouble(ThirdCharacter));
else
temp = (Double.parseDouble(FirstCharacter) - Double.parseDouble(ThirdCharacter));
FirstCharacter = Double.toString(temp); //Convert to string
SecondCharacter = GetCharacter;
ThirdCharacter = "";
}
Result.setText(FirstCharacter + SecondCharacter + ThirdCharacter); //Print result
}
}
}