计算机

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package Endclass.demol1;

import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;

public class AdvancedCalculator extends JFrame implements ActionListener {
private JButton[] numberButtons = new JButton[10];
private JButton addButton, subButton, mulButton, divButton, equalButton, clearButton;
private JButton powButton, sqrtButton, sinButton, cosButton, tanButton, historyButton;
private JTextField textField;
private String display = "";
private double tempFirst = 0;
private double tempSecond = 0;
private char operator;
private ArrayList<String> history = new ArrayList<>();
private JButton dotButton;

public AdvancedCalculator() {
createComponents();
setLayout(null);
setSize(300, 500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private void createComponents() {
textField = new JTextField();
textField.setBounds(50, 50, 200, 50);
add(textField);

addButton = new JButton("+");
subButton = new JButton("-");
mulButton = new JButton("*");
divButton = new JButton("/");
equalButton = new JButton("=");
clearButton = new JButton("C");

powButton = new JButton("^");
sqrtButton = new JButton("√");
sinButton = new JButton("sin");
cosButton = new JButton("cos");
tanButton = new JButton("tan");
historyButton = new JButton("History");
//附加
dotButton = new JButton(".");
dotButton.setBounds(150, 300, 50, 50);
add(dotButton);
//绑定事件
dotButton.addActionListener(this);

addButton.setBounds(200, 250, 50, 50);//+
subButton.setBounds(200, 200, 50, 50);//-
mulButton.setBounds(200, 150, 50, 50);//*
divButton.setBounds(200, 100, 50, 50);//÷
equalButton.setBounds(200, 300, 50, 50);//=
clearButton.setBounds(50, 100, 50, 50);//清空

powButton.setBounds(50, 300, 50, 50);
sqrtButton.setBounds(50, 350, 100, 50);
sinButton.setBounds(150, 350, 100, 50);
cosButton.setBounds(50, 400, 100, 50);
tanButton.setBounds(150, 400, 100, 50);
historyButton.setBounds(100, 100, 100, 50);

add(addButton);
add(subButton);
add(mulButton);
add(divButton);
add(equalButton);
add(clearButton);

add(powButton);
add(sqrtButton);
add(sinButton);
add(cosButton);
add(tanButton);
add(historyButton);

for (int i = 1; i < 10; i++) {
numberButtons[i] = new JButton(String.valueOf(i));
numberButtons[i].setBounds(50 + ((i-1) % 3) * 50, 150 + ((i-1) / 3) * 50, 50, 50);
add(numberButtons[i]);
numberButtons[i].addActionListener(this);
}
numberButtons[0]=new JButton(String.valueOf(0));
numberButtons[0].setBounds(100,300,50,50);
add(numberButtons[0]);
numberButtons[0].addActionListener(this);

addButton.addActionListener(this);
subButton.addActionListener(this);
mulButton.addActionListener(this);
divButton.addActionListener(this);
equalButton.addActionListener(this);
clearButton.addActionListener(this);

powButton.addActionListener(this);
sqrtButton.addActionListener(this);
sinButton.addActionListener(this);
cosButton.addActionListener(this);
tanButton.addActionListener(this);
historyButton.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {
display += i;
textField.setText(display);
return;
}
}

if (e.getSource() == addButton) {
tempFirst = Double.parseDouble(display);
operator = '+';
display = "";
} else if (e.getSource() == subButton) {
tempFirst = Double.parseDouble(display);
operator = '-';
display = "";
} else if (e.getSource() == mulButton) {
tempFirst = Double.parseDouble(display);
operator = '*';
display = "";
} else if (e.getSource() == divButton) {
tempFirst = Double.parseDouble(display);
operator = '/';
display = "";
} else if (e.getSource() == powButton) {
tempFirst = Double.parseDouble(display);
operator = '^';
display = "";
} else if (e.getSource() == sqrtButton) {
display = String.valueOf(Math.sqrt(Double.parseDouble(display)));
textField.setText(display);
history.add(display);
display = "";
} else if (e.getSource() == sinButton) {
display = String.valueOf(Math.sin(Math.toRadians(Double.parseDouble(display))));
textField.setText(display);
history.add(display);
display = "";
} else if (e.getSource() == cosButton) {
display = String.valueOf(Math.cos(Math.toRadians(Double.parseDouble(display))));
textField.setText(display);
history.add(display);
display = "";
} else if (e.getSource() == tanButton) {
display = String.valueOf(Math.tan(Math.toRadians(Double.parseDouble(display))));
textField.setText(display);
history.add(display);
display = "";
} else if (e.getSource() == equalButton) {
tempSecond = Double.parseDouble(display);
switch (operator) {
case '+':
display = String.valueOf(tempFirst + tempSecond);
break;
case '-':
display = String.valueOf(tempFirst - tempSecond);
break;
case '*':
display = String.valueOf(tempFirst * tempSecond);
break;
case '/':
display = String.valueOf(tempFirst / tempSecond);
break;
case '^':
display = String.valueOf(Math.pow(tempFirst, tempSecond));
break;
}
textField.setText(display);
history.add(display);
tempFirst = 0;
tempSecond = 0;
display = "";
} else if (e.getSource() == clearButton) {
textField.setText("");
display = "";
tempFirst = 0;
tempSecond = 0;
} else if (e.getSource() == historyButton) {
JOptionPane.showMessageDialog(this, String.join("\n", history));
}else if (e.getSource() == dotButton) {
if (!display.contains(".")) {
display += ".";
}
textField.setText(display);
}
}

public static void main(String[] args) {
new AdvancedCalculator();
}
}

代码运行截图

image-20231205212832973

下面四个是我另外加的,不过我感觉没有什么必要,你们不想要的可以删除

更新于

请我喝[茶]~( ̄▽ ̄)~*

罗梓丰 微信支付

微信支付

罗梓丰 支付宝

支付宝