-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwingCalculator.java
More file actions
108 lines (98 loc) · 3.75 KB
/
SwingCalculator.java
File metadata and controls
108 lines (98 loc) · 3.75 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SwingCalculator extends JFrame implements ActionListener {
private JTextField display;
private StringBuilder currentInput = new StringBuilder();
private double result = 0;
private String lastOperator = "=";
public SwingCalculator() {
setTitle("Swing Calculator");
setSize(350, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
// Display panel
display = new JTextField();
display.setFont(new Font("Arial", Font.BOLD, 32));
display.setHorizontalAlignment(SwingConstants.RIGHT);
display.setEditable(false);
display.setBackground(Color.WHITE);
display.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
add(display, BorderLayout.NORTH);
// Button panel
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4, 5, 5));
panel.setBackground(Color.DARK_GRAY);
String[] buttons = {
"C", "←", "%", "/",
"7", "8", "9", "*",
"4", "5", "6", "-",
"1", "2", "3", "+",
"±", "0", ".", "="
};
for (String text : buttons) {
JButton btn = new JButton(text);
btn.setFont(new Font("Arial", Font.BOLD, 24));
btn.setForeground(Color.WHITE);
btn.setBackground(Color.GRAY);
btn.setFocusPainted(false);
btn.addActionListener(this);
panel.add(btn);
}
add(panel, BorderLayout.CENTER);
getContentPane().setBackground(Color.BLACK);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.matches("[0-9]") || cmd.equals(".")) {
currentInput.append(cmd);
display.setText(currentInput.toString());
} else if (cmd.equals("C")) {
currentInput.setLength(0);
display.setText("");
result = 0;
lastOperator = "=";
} else if (cmd.equals("←")) {
if (currentInput.length() > 0) {
currentInput.deleteCharAt(currentInput.length() - 1);
display.setText(currentInput.toString());
}
} else if (cmd.equals("±")) {
if (currentInput.length() > 0) {
if (currentInput.charAt(0) == '-') {
currentInput.deleteCharAt(0);
} else {
currentInput.insert(0, '-');
}
display.setText(currentInput.toString());
}
} else if (cmd.equals("=")) {
calculate(Double.parseDouble(currentInput.toString()));
display.setText(String.valueOf(result));
currentInput.setLength(0);
lastOperator = "=";
} else { // operators +, -, *, /, %
if (currentInput.length() > 0) {
calculate(Double.parseDouble(currentInput.toString()));
currentInput.setLength(0);
}
lastOperator = cmd;
}
}
private void calculate(double number) {
switch (lastOperator) {
case "+": result += number; break;
case "-": result -= number; break;
case "*": result *= number; break;
case "/": result /= number; break;
case "%": result %= number; break;
case "=": result = number; break;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(SwingCalculator::new);
}
}