-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaCalculator.java
More file actions
132 lines (119 loc) · 4.45 KB
/
JavaCalculator.java
File metadata and controls
132 lines (119 loc) · 4.45 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
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class JavaCalculator extends JFrame implements ActionListener {
private final JTextField display = new JTextField();
private String current = "";
private String operator = "";
private double first = 0;
public JavaCalculator() {
try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception ignored) {}
setTitle("Swing Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(360, 520);
setLocationRelativeTo(null);
setLayout(new BorderLayout(10, 10));
((JComponent) getContentPane()).setBorder(new EmptyBorder(12, 12, 12, 12));
JLabel title = new JLabel("Calculator", SwingConstants.LEFT);
title.setFont(new Font("Segoe UI Semibold", Font.PLAIN, 20));
add(title, BorderLayout.PAGE_START);
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
display.setFont(new Font("Consolas", Font.PLAIN, 26));
display.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(70, 70, 70), 1, true),
new EmptyBorder(8, 10, 8, 10)));
display.setBackground(new Color(245, 245, 245));
add(display, BorderLayout.NORTH);
String[] buttons = {
"7","8","9","/","C",
"4","5","6","*","⌫",
"1","2","3","-","=",
"0",".","+/-","+","AC"
};
JPanel panel = new JPanel(new GridLayout(4, 5, 6, 6));
panel.setBackground(new Color(245, 245, 245));
Color numBg = new Color(230, 234, 238);
Color opBg = new Color(66, 133, 244);
Color opFg = Color.WHITE;
for (String text : buttons) {
JButton btn = new JButton(text);
btn.setFont(new Font("Segoe UI", Font.BOLD, 14));
btn.setFocusPainted(false);
btn.setMargin(new Insets(4, 6, 4, 6));
boolean isOp = "/-*+=C⌫AC+/-".contains(text);
btn.setBackground(isOp ? opBg : numBg);
btn.setForeground(isOp ? opFg : Color.DARK_GRAY);
btn.addActionListener(this);
panel.add(btn);
}
add(panel, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = ((JButton) e.getSource()).getText();
switch (cmd) {
case "C":
current = "";
display.setText("");
break;
case "AC":
current = "";
operator = "";
first = 0;
display.setText("");
break;
case "⌫":
if (!current.isEmpty()) {
current = current.substring(0, current.length() - 1);
display.setText(current);
}
break;
case "+/-":
if (!current.isEmpty()) {
if (current.startsWith("-")) current = current.substring(1);
else current = "-" + current;
display.setText(current);
}
break;
case "+": case "-": case "*": case "/":
commitCurrent();
operator = cmd;
current = "";
break;
case "=":
commitCurrent();
operator = "";
current = String.valueOf(first);
display.setText(current);
break;
default: // digits or dot
current += cmd;
display.setText(current);
}
}
private void commitCurrent() {
if (!current.isEmpty()) {
double val = Double.parseDouble(current);
if (operator.isEmpty()) {
first = val;
} else {
first = apply(first, val, operator);
}
}
}
private double apply(double a, double b, String op) {
switch (op) {
case "+": return a + b;
case "-": return a - b;
case "*": return a * b;
case "/": return b == 0 ? 0 : a / b;
default: return b;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new JavaCalculator().setVisible(true));
}
}