-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasiccalculator.java
More file actions
137 lines (112 loc) · 3.3 KB
/
basiccalculator.java
File metadata and controls
137 lines (112 loc) · 3.3 KB
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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class basiccalculator extends JFrame {
private final long serialVersionUID = 1L;
private final Font BIGGER_FONT=new Font("monspaced",Font.ITALIC,24);
private JTextField textfield;
private boolean number=true;
private String equalop="=";
//calls calculatorop class for operator
private CalculatorOp op = new CalculatorOp();
public basiccalculator()//constructor start
{
textfield=new JTextField("0",12);
textfield.setHorizontalAlignment(JTextField.LEFT);
textfield.setFont(BIGGER_FONT);
////////////////////////////////////
// textfield2=new JTextField("0",12);
// textfield2.setHorizontalAlignment(JTextField.RIGHT);
// textfield2.setFont(BIGGER_FONT);
//calls numberlistner class
ActionListener numberlistner=new NumberListner();
String buttonorder="1234567890 ";
JPanel buttonpanel=new JPanel();
buttonpanel.setLayout(new GridLayout(4,3,3,2));
for (int i = 0; i < buttonorder.length(); i++) {
String key=buttonorder.substring(i, i+1);
if(key.equals(" ")){
buttonpanel.add(new JLabel(""));
}//if
else
{
JButton button=new JButton(key);
button.addActionListener(numberlistner);
button.setFont(BIGGER_FONT);
buttonpanel.add(button);
}//else
}//for finished
//calls operatorlistner class
ActionListener operatorlistner=new OperatorListner();
JPanel panel=new JPanel();
panel.setLayout(new GridLayout(4,4,4,4));
String[] oporder={"Cler","+","-","*","/","Anser"};
for (int i = 0; i < oporder.length; i++) {
JButton button=new JButton(oporder[i]);
button.addActionListener(operatorlistner);
button.setFont(BIGGER_FONT);
panel.add(button);
}//for inished
JPanel pan=new JPanel();
pan.setLayout(new BorderLayout(4,4));
pan.add(textfield,BorderLayout.NORTH);
pan.add(buttonpanel,BorderLayout.CENTER);
pan.add(panel,BorderLayout.EAST);
this.setContentPane(pan);
this.pack();
this.setTitle("Calculator BY ankit");
//this.setResizable(false);
}//constructor finished
private void action()
{
number=true;
textfield.setText("0");
equalop= "=";
op.setTotal("0");
}
class OperatorListner implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(number)
{
action();
textfield.setText("0");
}
else
{
number=true;
String displaytext=textfield.getText();
if(equalop.equals("="))
{
op.setTotal(displaytext);
}else if(equalop.equals("+")){
op.add(displaytext);
}else if(equalop.equals("-"))
{
op.subtarct(displaytext);
}else if(equalop.equals("*"))
{
op.multiply(displaytext);
}else if(equalop.equals("/"))
{
op.divide(displaytext);
}
textfield.setText("" + op.GetTotalString());
equalop = e.getActionCommand();
}
}//method finished
}
//operatorlistner class finished
class NumberListner implements ActionListener{
public void actionPerformed(ActionEvent e) {
String digit=e.getActionCommand();
if(number)
{
textfield.setText(digit);
number=false;
}else{
textfield.setText(textfield.getText()+digit);
}
}//method
}//numberlistner finished
}