用java gui编写一个实现加减乘除和求余的计算器,旁边有计算的历史记录上面的保存按钮可以保存到电脑文件里,还可以通过清除按钮清除历史记录
时间: 2024-01-22 22:19:12 浏览: 58
好的,我可以为您提供一个简单的Java GUI计算器示例,它将实现加减乘除和求余操作,并具有保存历史记录和清除历史记录的功能。下面是完整的代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField textField;
private JButton addButton, subtractButton, multiplyButton, divideButton, moduloButton, clearButton, saveButton;
private JPanel buttonPanel;
private String history = "";
public Calculator() {
setTitle("Calculator");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
textField = new JTextField();
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
addButton = new JButton("+");
subtractButton = new JButton("-");
multiplyButton = new JButton("*");
divideButton = new JButton("/");
moduloButton = new JButton("%");
clearButton = new JButton("Clear");
saveButton = new JButton("Save");
addButton.addActionListener(this);
subtractButton.addActionListener(this);
multiplyButton.addActionListener(this);
divideButton.addActionListener(this);
moduloButton.addActionListener(this);
clearButton.addActionListener(this);
saveButton.addActionListener(this);
buttonPanel = new JPanel(new GridLayout(3, 3));
buttonPanel.add(addButton);
buttonPanel.add(subtractButton);
buttonPanel.add(multiplyButton);
buttonPanel.add(divideButton);
buttonPanel.add(moduloButton);
buttonPanel.add(clearButton);
buttonPanel.add(saveButton);
for (int i = 0; i < 10; i++) {
JButton button = new JButton(String.valueOf(i));
button.addActionListener(this);
buttonPanel.add(button);
}
add(textField, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("+")) {
performOperation("+");
} else if (command.equals("-")) {
performOperation("-");
} else if (command.equals("*")) {
performOperation("*");
} else if (command.equals("/")) {
performOperation("/");
} else if (command.equals("%")) {
performOperation("%");
} else if (command.equals("Clear")) {
clearHistory();
} else if (command.equals("Save")) {
saveHistory();
} else {
textField.setText(textField.getText() + command);
}
}
private void performOperation(String operator) {
try {
double result = 0;
double number = Double.parseDouble(textField.getText());
textField.setText("");
switch (operator) {
case "+":
result = Double.parseDouble(history) + number;
break;
case "-":
result = Double.parseDouble(history) - number;
break;
case "*":
result = Double.parseDouble(history) * number;
break;
case "/":
result = Double.parseDouble(history) / number;
break;
case "%":
result = Double.parseDouble(history) % number;
break;
}
history += number + " " + operator + " ";
textField.setText(String.valueOf(result));
} catch (NumberFormatException ex) {
textField.setText("Invalid input");
}
}
private void clearHistory() {
history = "";
}
private void saveHistory() {
try {
String fileName = JOptionPane.showInputDialog("Enter file name:");
FileWriter writer = new FileWriter(fileName);
writer.write(history);
writer.close();
JOptionPane.showMessageDialog(null, "History saved to file " + fileName);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error saving file");
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
这个程序使用了Java的Swing库来构建GUI。它包含一个JTextField,用于显示计算器的输入和输出,以及一个JPanel,包含所有的按钮。每个按钮都添加了一个ActionListener,以便在单击时执行相应的操作。
当用户单击加、减、乘、除或求余按钮时,程序将从输入框中读取一个数字,并将其与历史记录中的数字进行相应的运算。结果将显示在输入框中,并将更新历史记录。
用户可以通过单击"Clear"按钮来清除历史记录,或者通过单击"Save"按钮将历史记录保存到文件中。程序将提示用户输入文件名,并将历史记录写入到指定的文件中。如果保存过程出现错误,程序将显示一条错误消息。
这个程序只是一个简单的示例,您可以根据自己的需求进行修改和扩展。
阅读全文