-
Notifications
You must be signed in to change notification settings - Fork 0
/
program 13 (calculator)
151 lines (128 loc) · 4.9 KB
/
program 13 (calculator)
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
Question : Write a Java program to
create a simple calculator
using java AWT elements.
.Use a grid layout to
arrange buttons for the
digits and
basic operation +, -, /, *.
Add a text felid to display
the
results.
code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BuildCalculator extends JFrame implements ActionListener{
JFrame actualWindow;
JPanel resultPanel, buttonPanel, infoPanel;
JTextField resultTxt;
JButton btn_digits[] = new JButton[10];
JButton btn_plus, btn_minus, btn_mul, btn_div, btn_equal, btn_dot, btn_clear;
char eventFrom;
JLabel expression, appTitle, siteTitle ;
double oparand_1 = 0, operand_2 = 0;
String operator = "=";
BuildCalculator() {
Font txtFont = new Font("SansSerif", Font.BOLD, 20);
Font titleFont = new Font("SansSerif", Font.BOLD, 30);
Font expressionFont = new Font("SansSerif", Font.BOLD, 15);
actualWindow = new JFrame("Calculator");
resultPanel = new JPanel();
buttonPanel = new JPanel();
infoPanel = new JPanel();
actualWindow.setLayout(new GridLayout(3, 1));
buttonPanel.setLayout(new GridLayout(4, 4));
infoPanel.setLayout(new GridLayout(3, 1));
actualWindow.setResizable(false);
appTitle = new JLabel("My Calculator");
appTitle.setFont(titleFont);
expression = new JLabel("Expression shown here");
expression.setFont(expressionFont);
siteTitle = new JLabel("SARVADNYA AWAGHAD D10A 05");
siteTitle.setFont(expressionFont);
siteTitle.setHorizontalAlignment(SwingConstants.CENTER);
siteTitle.setForeground(Color.BLUE);
resultTxt = new JTextField(15);
resultTxt.setBorder(null);
resultTxt.setPreferredSize(new Dimension(15, 50));
resultTxt.setFont(txtFont);
resultTxt.setHorizontalAlignment(SwingConstants.RIGHT);
for(int i = 0; i < 10; i++) {
btn_digits[i] = new JButton(""+i);
btn_digits[i].addActionListener(this);
}
btn_plus = new JButton("+");
btn_plus.addActionListener(this);
btn_minus = new JButton("-");
btn_minus.addActionListener(this);
btn_mul = new JButton("*");
btn_mul.addActionListener(this);
btn_div = new JButton("/");
btn_div.addActionListener(this);
btn_dot = new JButton(".");
btn_dot.addActionListener(this);
btn_equal = new JButton("=");
btn_equal.addActionListener(this);
btn_clear = new JButton("Clear");
btn_clear.addActionListener(this);
resultPanel.add(appTitle);
resultPanel.add(resultTxt);
resultPanel.add(expression);
for(int i = 0; i < 10; i++) {
buttonPanel.add(btn_digits[i]);
}
buttonPanel.add(btn_plus);
buttonPanel.add(btn_minus);
buttonPanel.add(btn_mul);
buttonPanel.add(btn_div);
buttonPanel.add(btn_dot);
buttonPanel.add(btn_equal);
infoPanel.add(btn_clear);
infoPanel.add(siteTitle);
actualWindow.add(resultPanel);
actualWindow.add(buttonPanel);
actualWindow.add(infoPanel);
actualWindow.setSize(300, 500);
actualWindow.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
eventFrom = e.getActionCommand().charAt(0);
String buildNumber;
if(Character.isDigit(eventFrom)) {
buildNumber = resultTxt.getText() + eventFrom;
resultTxt.setText(buildNumber);
} else if(e.getActionCommand() == ".") {
buildNumber = resultTxt.getText() + eventFrom;
resultTxt.setText(buildNumber);
}
else if(eventFrom != '='){
oparand_1 = Double.parseDouble(resultTxt.getText());
operator = e.getActionCommand();
expression.setText(oparand_1 + " " + operator);
resultTxt.setText("");
} else if(e.getActionCommand() == "Clear") {
resultTxt.setText("");
}
else {
operand_2 = Double.parseDouble(resultTxt.getText());
expression.setText(expression.getText() + " " + operand_2);
switch(operator) {
case "+": resultTxt.setText(""+(oparand_1 + operand_2)); break;
case "-": resultTxt.setText(""+(oparand_1 - operand_2)); break;
case "*": resultTxt.setText(""+(oparand_1 * operand_2)); break;
case "/": try {
if(operand_2 == 0)
throw new ArithmeticException();
resultTxt.setText(""+(oparand_1 / operand_2)); break;
} catch(ArithmeticException ae) {
JOptionPane.showMessageDialog(actualWindow, "Divisor can not be ZERO");
}
}
}
}
}
public class Calculator {
public static void main(String[] args) {
new BuildCalculator();
}
}