forked from waboke/calculator_app_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwingCalculator.java
More file actions
89 lines (78 loc) · 3.41 KB
/
SwingCalculator.java
File metadata and controls
89 lines (78 loc) · 3.41 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SwingCalculator {
public static void main(String[] args) {
JFrame frame = new JFrame("Arithmetic Functions");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 2, 10, 10));
JLabel label1 = new JLabel("Enter the first number:");
JTextField textField1 = new JTextField();
JLabel label2 = new JLabel("Enter the second number:");
JTextField textField2 = new JTextField();
JButton addButton = new JButton("Addition");
JButton subtractButton = new JButton("Subtraction");
JButton multiplyButton = new JButton("Multiplication");
JButton exitButton = new JButton("Exit");
JLabel resultLabel = new JLabel("Result: ");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(textField1.getText());
double num2 = Double.parseDouble(textField2.getText());
double result = num1 + num2;
resultLabel.setText("Result: " + result);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter valid numbers.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
subtractButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(textField1.getText());
double num2 = Double.parseDouble(textField2.getText());
double result = num1 - num2;
resultLabel.setText("Result: " + result);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter valid numbers.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
multiplyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
double num1 = Double.parseDouble(textField1.getText());
double num2 = Double.parseDouble(textField2.getText());
double result = num1 * num2;
resultLabel.setText("Result: " + result);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(frame, "Please enter valid numbers.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
exitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(label1);
panel.add(textField1);
panel.add(label2);
panel.add(textField2);
panel.add(addButton);
panel.add(subtractButton);
panel.add(multiplyButton);
panel.add(exitButton);
panel.add(resultLabel);
frame.add(panel);
frame.setVisible(true);
}
}