-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.java
152 lines (132 loc) · 5.12 KB
/
calculator.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class calculator extends JFrame
{
// declare GUI components
private JTextField num1Field, num2Field;
private JButton addButton, subtractButton, multiplyButton, divideButton;
private JLabel resultLabel;
//set constructor
public calculator()
{
// Create the components
num1Field = new JTextField(10);
num2Field = new JTextField(10);
addButton = new JButton("+");
subtractButton = new JButton("-");
multiplyButton = new JButton("*");
divideButton = new JButton("/");
resultLabel = new JLabel("Result: ");
//add components to the window
setLayout(new FlowLayout());
add(new JLabel("Number 1: "));
add(num1Field);
add(new JLabel("Number 2: "));
add(num2Field);
add(addButton);
add(subtractButton);
add(multiplyButton);
add(divideButton);
add(resultLabel);
//add action listener to the add buttons
addButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//decalare variables
double num1, num2, result;
try {
// Get the numbers from the text fields
num1 = Double.parseDouble(num1Field.getText());
num2 = Double.parseDouble(num2Field.getText());
}
catch (NumberFormatException ex)
{
resultLabel.setText("Error: Invalid input");
return;
}
// Perform the calculation based on the button clicked
if (e.getSource() == addButton)
{
resultLabel.setText("Result: " + (num1 + num2));
}
}
});
//add action listener to the add buttons
subtractButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//decalare variables
double num1, num2, result;
try {
// Get the numbers from the text fields
num1 = Double.parseDouble(num1Field.getText());
num2 = Double.parseDouble(num2Field.getText());
}
catch (NumberFormatException ex)
{
resultLabel.setText("Error: Invalid input");
return;
}
// Perform the calculation based on the button clicked
if (e.getSource() == subtractButton)
{
resultLabel.setText("Result: " + (num1 - num2));
}
}
});
//add action listener to the add buttons
multiplyButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//decalare variables
double num1, num2, result;
try {
// Get the numbers from the text fields
num1 = Double.parseDouble(num1Field.getText());
num2 = Double.parseDouble(num2Field.getText());
}
catch (NumberFormatException ex)
{
resultLabel.setText("Error: Invalid input");
return;
}
// Perform the calculation based on the button clicked
if (e.getSource() == multiplyButton)
{
resultLabel.setText("Result: " + (num1 * num2));
}
}
});
//add action listener to the add buttons
divideButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//decalare variables
double num1, num2, result;
try {
// Get the numbers from the text fields
num1 = Double.parseDouble(num1Field.getText());
num2 = Double.parseDouble(num2Field.getText());
}
catch (NumberFormatException ex)
{
resultLabel.setText("Error: Invalid input");
return;
}
// Perform the calculation based on the button clicked
if (e.getSource() == divideButton)
{
resultLabel.setText("Result: " + (num1 / num2));
}
}
});
// set window size and make it visible
setSize(500, 500);
setVisible(true);
}
}