@Override public void actionPerformed(ActionEvent e) {//触发此动作时执行 for (int i = 0; i < 10; i++) { if (e.getSource() == numberButtons[i]) {//返回最初发生 Event 的对象,那个对象被触发,就返回这个对象 textField.setText(textField.getText().concat(String.valueOf(i))); } } if (e.getSource() == decButton) { textField.setText(textField.getText().concat(".")); } if (e.getSource() == addButton) { num1 = Double.parseDouble(textField.getText()); operator = '+'; textField.setText(""); } if (e.getSource() == subButton) { num1 = Double.parseDouble(textField.getText()); operator = '-'; textField.setText(""); } if (e.getSource() == mulButton) { num1 = Double.parseDouble(textField.getText()); operator = '*'; textField.setText(""); } if (e.getSource() == divButton) { num1 = Double.parseDouble(textField.getText()); operator = '/'; textField.setText(""); } if (e.getSource() == equButton) { num2 = Double.parseDouble(textField.getText()); switch (operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; } textField.setText(String.valueOf(result)); num1 = result; } if (e.getSource() == clrButton) { textField.setText(""); } if (e.getSource() == delButton) { String string = textField.getText(); textField.setText(""); for (int i = 0; i < string.length() - 1; i++) { textField.setText(textField.getText() + string.charAt(i)); } } } }请注释出每一步在做什么
时间: 2024-01-14 12:05:08 浏览: 100
这段代码实现了一个简单的计算器的基本功能,以下是每一步的注释:
```java
@Override
public void actionPerformed(ActionEvent e) {//触发此动作时执行
// 为数字按钮添加事件,将数字添加到文本框中
for (int i = 0; i < 10; i++) {
if (e.getSource() == numberButtons[i]) {//返回最初发生 Event 的对象,那个对象被触发,就返回这个对象
textField.setText(textField.getText().concat(String.valueOf(i)));
}
}
// 为小数点按钮添加事件,将小数点添加到文本框中
if (e.getSource() == decButton) {
textField.setText(textField.getText().concat("."));
}
// 为加号按钮添加事件,记录第一个数,将文本框清空,记录运算符
if (e.getSource() == addButton) {
num1 = Double.parseDouble(textField.getText());
operator = '+';
textField.setText("");
}
// 为减号按钮添加事件,记录第一个数,将文本框清空,记录运算符
if (e.getSource() == subButton) {
num1 = Double.parseDouble(textField.getText());
operator = '-';
textField.setText("");
}
// 为乘号按钮添加事件,记录第一个数,将文本框清空,记录运算符
if (e.getSource() == mulButton) {
num1 = Double.parseDouble(textField.getText());
operator = '*';
textField.setText("");
}
// 为除号按钮添加事件,记录第一个数,将文本框清空,记录运算符
if (e.getSource() == divButton) {
num1 = Double.parseDouble(textField.getText());
operator = '/';
textField.setText("");
}
// 为等于号按钮添加事件,记录第二个数,根据运算符计算结果并显示在文本框中,将结果记录为第一个数
if (e.getSource() == equButton) {
num2 = Double.parseDouble(textField.getText());
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
textField.setText(String.valueOf(result));
num1 = result;
}
// 为清除按钮添加事件,将文本框清空
if (e.getSource() == clrButton) {
textField.setText("");
}
// 为删除按钮添加事件,删除文本框中最后一个字符
if (e.getSource() == delButton) {
String string = textField.getText();
textField.setText("");
for (int i = 0; i < string.length() - 1; i++) {
textField.setText(textField.getText() + string.charAt(i));
}
}
}
```
阅读全文