-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcu.java
92 lines (80 loc) · 3 KB
/
Calcu.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calcu extends JFrame implements ActionListener {
JTextField display;
double num1 = 0, num2 = 0, result = 0;
char operator;
public Calcu() {
setTitle("Calculator");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null); // center the window
// Display
display = new JTextField();
display.setEditable(false);
display.setFont(new Font("Segoe UI", Font.BOLD, 30));
display.setBackground(Color.BLACK);
display.setForeground(Color.GREEN);
display.setHorizontalAlignment(JTextField.RIGHT);
display.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
add(display, BorderLayout.NORTH);
// Button Panel
JPanel panel = new JPanel(new GridLayout(4, 4, 10, 10));
panel.setBackground(Color.DARK_GRAY);
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
String[] buttons = {
"7", "8", "9", "+",
"4", "5", "6", "-",
"1", "2", "3", "*",
"C", "0", "=", "/"
};
for (String text : buttons) {
JButton button = new JButton(text);
button.setFont(new Font("Segoe UI", Font.BOLD, 22));
button.setFocusPainted(false);
button.setBackground(isOperator(text) ? new Color(255, 140, 0) : Color.LIGHT_GRAY);
button.setForeground(Color.BLACK);
button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
button.addActionListener(this);
panel.add(button);
}
add(panel, BorderLayout.CENTER);
setVisible(true);
}
private boolean isOperator(String text) {
return "+-*/=".contains(text);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.charAt(0) >= '0' && command.charAt(0) <= '9') {
display.setText(display.getText() + command);
} else if (command.equals("C")) {
display.setText("");
num1 = num2 = result = 0;
} else if (command.equals("=")) {
num2 = Double.parseDouble(display.getText());
switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/':
if (num2 != 0) result = num1 / num2;
else {
display.setText("Error");
return;
}
break;
}
display.setText(String.valueOf(result));
} else {
num1 = Double.parseDouble(display.getText());
operator = command.charAt(0);
display.setText("");
}
}
public static void main(String[] args) {
new Calcu();
}
}