用java制作一个简易计算器。比如:按键得到23*2.2 单机“=”得出结果50.6 。“MC键实现清 除右侧的显示内容。 简易计算器用图形显示出
时间: 2024-03-14 13:46:46 浏览: 116
好的,我可以为您提供一个Java Swing GUI实现的简易计算器。下面是代码示例:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Calculator {
private JFrame frame;
private JTextField textField;
private double num1 = 0.0;
private double num2 = 0.0;
private String operator = "";
public Calculator() {
frame = new JFrame("简易计算器");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
frame.setLayout(new BorderLayout());
// 显示结果的文本框
textField = new JTextField();
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
frame.add(textField, BorderLayout.NORTH);
// 数字和运算符按钮面板
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
// 数字按钮
for (int i = 1; i <= 9; i++) {
JButton button = new JButton(String.valueOf(i));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = textField.getText() + button.getText();
textField.setText(text);
}
});
buttonPanel.add(button);
}
// 清除按钮
JButton clearButton = new JButton("C");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
textField.setText("");
num1 = 0.0;
num2 = 0.0;
operator = "";
}
});
buttonPanel.add(clearButton);
// 零按钮
JButton zeroButton = new JButton("0");
zeroButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = textField.getText() + zeroButton.getText();
textField.setText(text);
}
});
buttonPanel.add(zeroButton);
// 小数点按钮
JButton dotButton = new JButton(".");
dotButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = textField.getText() + dotButton.getText();
textField.setText(text);
}
});
buttonPanel.add(dotButton);
// 加按钮
JButton addButton = new JButton("+");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "+";
textField.setText("");
}
});
buttonPanel.add(addButton);
// 减按钮
JButton subtractButton = new JButton("-");
subtractButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "-";
textField.setText("");
}
});
buttonPanel.add(subtractButton);
// 乘按钮
JButton multiplyButton = new JButton("*");
multiplyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "*";
textField.setText("");
}
});
buttonPanel.add(multiplyButton);
// 除按钮
JButton divideButton = new JButton("/");
divideButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num1 = Double.parseDouble(textField.getText());
operator = "/";
textField.setText("");
}
});
buttonPanel.add(divideButton);
frame.add(buttonPanel, BorderLayout.CENTER);
// 等号按钮
JButton equalsButton = new JButton("=");
equalsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num2 = Double.parseDouble(textField.getText());
double result = 0.0;
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));
}
});
frame.add(equalsButton, BorderLayout.SOUTH);
frame.setVisible(true);
}
public static void main(String[] args) {
Calculator calculator = new Calculator();
}
}
```
运行这段代码,将会得到一个简易计算器的GUI界面。您可以通过数字按钮输入数字,通过加、减、乘、除按钮输入运算符,然后按下等号按钮计算结果。
如果您想要在这个示例中添加MC键来清除右侧的显示内容,可以在数字和运算符按钮面板中添加一个MC按钮,然后为它添加一个ActionListener来清除文本框的内容。
阅读全文