-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmeticCalculatorGUI.java
More file actions
123 lines (108 loc) · 5.05 KB
/
ArithmeticCalculatorGUI.java
File metadata and controls
123 lines (108 loc) · 5.05 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ArithmeticCalculatorGUI {
private JFrame frame;
private JTextField textField;
private double num1 = 0, num2 = 0, result = 0;
private String operator = "";
// Main method to launch the GUI application
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
try {
ArithmeticCalculatorGUI window = new ArithmeticCalculatorGUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
// Constructor to initialize the components and GUI
public ArithmeticCalculatorGUI() {
// Initialize the frame
frame = new JFrame("Arithmetic Calculator");
frame.setBounds(100, 100, 400, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout());
// Initialize the text field to display numbers and results (larger size and styling)
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.PLAIN, 36)); // Glorified font size for result
textField.setBounds(10, 10, 360, 80); // Larger height for result area
textField.setColumns(10);
textField.setHorizontalAlignment(JTextField.RIGHT); // Align text to the right for better clarity
textField.setBackground(new Color(230, 230, 250)); // Light lavender background color for the text field
textField.setForeground(new Color(0, 0, 0)); // Black text for better contrast
textField.setBorder(BorderFactory.createLineBorder(new Color(70, 130, 180), 2)); // Blue border to glorify
frame.getContentPane().add(textField, BorderLayout.NORTH);
// Create a panel for the calculator buttons
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 5, 5)); // 4 rows, 4 columns, 5px padding between buttons
// Create number buttons (0-9) and operator buttons (+, -, *, /, =)
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
// Add buttons to the panel with action listeners
for (String text : buttons) {
JButton button = new JButton(text);
button.setFont(new Font("Tahoma", Font.PLAIN, 14)); // Smaller font size for buttons
button.setPreferredSize(new Dimension(40, 40)); // Set all buttons to a smaller size (40x40)
button.setBackground(new Color(240, 240, 240)); // Light gray background for buttons
button.setForeground(new Color(0, 0, 0)); // Black text for buttons
button.setBorder(BorderFactory.createLineBorder(new Color(100, 100, 100), 1)); // Gray border for buttons
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
handleButtonClick(e);
}
});
panel.add(button);
}
// Add the panel with buttons to the frame
frame.getContentPane().add(panel, BorderLayout.CENTER);
}
// Handle button clicks for numbers and operations
private void handleButtonClick(ActionEvent e) {
String command = e.getActionCommand();
// If the button is a number or decimal point, update the text field
if ((command.charAt(0) >= '0' && command.charAt(0) <= '9') || command.charAt(0) == '.') {
textField.setText(textField.getText() + command);
}
// If the button is an operator (+, -, *, /)
else if (command.equals("=")) {
// Perform calculation when "=" is pressed
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 == 0) {
JOptionPane.showMessageDialog(frame, "Cannot divide by zero", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
result = num1 / num2;
break;
}
// Display the result
textField.setText(String.valueOf(result));
operator = ""; // Clear operator for the next calculation
} else {
// Store the operator and the first number
if (!textField.getText().equals("")) {
num1 = Double.parseDouble(textField.getText());
textField.setText("");
operator = command;
}
}
}
}