用java编写:实现一个简单的计算器,前面是一个文本框(操作数1),后面是标签(操作符),再后面是一个文本框(操作数2),然后是一个"="按钮(JButton),最后是一个文本框(结果) (1)实现按钮ActionEvent事件的处理,点击等号获取计算结果,并显示在最后一个文本框; (2)实现第二个文本框的ActionEvent事件处理,在第二个文本框按回车,直接获得计算结果,并显示在最后一个文本框。(事件源为第二个文本框) (3)实现运算符标签的MouseEvent事件的处理,点击标签,可以更换标签的运算符,比如:将加号改成减号,从而实现减法功能,点击等号按钮,实现减法运算,并将结果显示在第三个文本框中。(具体实现:实现mouseClicked()时,判断鼠标单击次数是否为除4余0、余1、余2、余3,比如余为1,则将运算符标签上的字符修改为”-”,从而实现减法运算)
时间: 2024-02-24 17:56:47 浏览: 63
以下是实现简单计算器的Java代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Calculator extends JFrame implements ActionListener, MouseListener {
private JTextField operand1Field, operand2Field, resultField;
private JLabel operatorLabel;
private JButton equalsButton;
public Calculator() {
super("Calculator");
// Create UI components
operand1Field = new JTextField(10);
operatorLabel = new JLabel("+");
operatorLabel.addMouseListener(this);
operand2Field = new JTextField(10);
operand2Field.addActionListener(this);
equalsButton = new JButton("=");
equalsButton.addActionListener(this);
resultField = new JTextField(10);
resultField.setEditable(false);
// Add components to content pane
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(operand1Field);
contentPane.add(operatorLabel);
contentPane.add(operand2Field);
contentPane.add(equalsButton);
contentPane.add(resultField);
// Set window properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == equalsButton || e.getSource() == operand2Field) {
try {
int operand1 = Integer.parseInt(operand1Field.getText());
int operand2 = Integer.parseInt(operand2Field.getText());
int result = 0;
switch (operatorLabel.getText()) {
case "+":
result = operand1 + operand2;
break;
case "-":
result = operand1 - operand2;
break;
case "*":
result = operand1 * operand2;
break;
case "/":
result = operand1 / operand2;
break;
}
resultField.setText(Integer.toString(result));
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "Invalid input!");
} catch (ArithmeticException ex) {
JOptionPane.showMessageDialog(this, "Division by zero!");
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() % 4 == 1) {
operatorLabel.setText("-");
} else if (e.getClickCount() % 4 == 2) {
operatorLabel.setText("*");
} else if (e.getClickCount() % 4 == 3) {
operatorLabel.setText("/");
} else {
operatorLabel.setText("+");
}
}
// Unused methods from MouseListener interface
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
public static void main(String[] args) {
new Calculator();
}
}
```
这个计算器支持加、减、乘、除四种运算,可以通过点击运算符标签来切换运算符。它还会检查输入的操作数是否合法,并捕获除零错误。
阅读全文