请帮我完成一下我的java作业,作业内容是:用java制作一个标准型计算器,界面功能参考windows7计算器,使用了gui图形化的,能实现加减乘除,平方,开方等算数运算的基本功能,菜单里面有历史记录选项可以显示历次计算的历史记录,可以吧技术记录或结果存储到文件中,可以从文件中读取到历史记录中,代码内容看起来要像初学者制作的,把代码内容分为3个类,
时间: 2023-06-25 16:05:04 浏览: 109
基于Java实现的(GUI)简易计算器【100012745】
5星 · 资源好评率100%
好的,我可以帮你完成这个作业。首先,我们需要创建三个类:Calculator、History和Record。
Calculator类用于创建GUI界面和实现计算器的基本功能,如加减乘除,平方,开方等算数运算。其中还包括菜单项,用于存储历史记录和从文件中读取历史记录。下面是Calculator类的代码:
```
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField display;
private JPanel buttonPanel;
private JButton zeroButton, oneButton, twoButton, threeButton, fourButton, fiveButton, sixButton, sevenButton, eightButton, nineButton;
private JButton addButton, subtractButton, multiplyButton, divideButton, equalsButton, clearButton, squareButton, rootButton;
private JMenu fileMenu;
private JMenuItem saveItem, openItem;
private History history;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
setResizable(false);
display = new JTextField(20);
display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4));
zeroButton = new JButton("0");
oneButton = new JButton("1");
twoButton = new JButton("2");
threeButton = new JButton("3");
fourButton = new JButton("4");
fiveButton = new JButton("5");
sixButton = new JButton("6");
sevenButton = new JButton("7");
eightButton = new JButton("8");
nineButton = new JButton("9");
addButton = new JButton("+");
subtractButton = new JButton("-");
multiplyButton = new JButton("*");
divideButton = new JButton("/");
equalsButton = new JButton("=");
clearButton = new JButton("C");
squareButton = new JButton("x^2");
rootButton = new JButton("sqrt");
zeroButton.addActionListener(this);
oneButton.addActionListener(this);
twoButton.addActionListener(this);
threeButton.addActionListener(this);
fourButton.addActionListener(this);
fiveButton.addActionListener(this);
sixButton.addActionListener(this);
sevenButton.addActionListener(this);
eightButton.addActionListener(this);
nineButton.addActionListener(this);
addButton.addActionListener(this);
subtractButton.addActionListener(this);
multiplyButton.addActionListener(this);
divideButton.addActionListener(this);
equalsButton.addActionListener(this);
clearButton.addActionListener(this);
squareButton.addActionListener(this);
rootButton.addActionListener(this);
buttonPanel.add(sevenButton);
buttonPanel.add(eightButton);
buttonPanel.add(nineButton);
buttonPanel.add(divideButton);
buttonPanel.add(fourButton);
buttonPanel.add(fiveButton);
buttonPanel.add(sixButton);
buttonPanel.add(multiplyButton);
buttonPanel.add(oneButton);
buttonPanel.add(twoButton);
buttonPanel.add(threeButton);
buttonPanel.add(subtractButton);
buttonPanel.add(zeroButton);
buttonPanel.add(clearButton);
buttonPanel.add(equalsButton);
buttonPanel.add(addButton);
buttonPanel.add(squareButton);
buttonPanel.add(rootButton);
add(buttonPanel, BorderLayout.CENTER);
history = new History();
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
fileMenu = new JMenu("File");
menuBar.add(fileMenu);
saveItem = new JMenuItem("Save History");
saveItem.addActionListener(this);
fileMenu.add(saveItem);
openItem = new JMenuItem("Open History");
openItem.addActionListener(this);
fileMenu.add(openItem);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String button = e.getActionCommand();
String currentText = display.getText();
if (button.equals("C")) {
display.setText("");
} else if (button.equals("+")) {
history.addRecord(currentText);
display.setText("");
history.addRecord("+");
} else if (button.equals("-")) {
history.addRecord(currentText);
display.setText("");
history.addRecord("-");
} else if (button.equals("*")) {
history.addRecord(currentText);
display.setText("");
history.addRecord("*");
} else if (button.equals("/")) {
history.addRecord(currentText);
display.setText("");
history.addRecord("/");
} else if (button.equals("=")) {
history.addRecord(currentText);
String result = calculate(history.getRecords());
display.setText(result);
history.addRecord(result);
} else if (button.equals("x^2")) {
double value = Double.parseDouble(currentText);
double result = value * value;
display.setText(Double.toString(result));
history.addRecord(currentText);
history.addRecord("^2");
history.addRecord(Double.toString(result));
} else if (button.equals("sqrt")) {
double value = Double.parseDouble(currentText);
double result = Math.sqrt(value);
display.setText(Double.toString(result));
history.addRecord(currentText);
history.addRecord("sqrt");
history.addRecord(Double.toString(result));
} else if (button.equals("Save History")) {
history.saveToFile();
} else if (button.equals("Open History")) {
history.loadFromFile();
} else {
display.setText(currentText + button);
}
}
private String calculate(String[] records) {
double result = Double.parseDouble(records[0]);
for (int i = 1; i < records.length - 1; i += 2) {
double value = Double.parseDouble(records[i + 1]);
switch (records[i]) {
case "+":
result += value;
break;
case "-":
result -= value;
break;
case "*":
result *= value;
break;
case "/":
result /= value;
break;
default:
break;
}
}
return Double.toString(result);
}
public static void main(String[] args) {
new Calculator();
}
}
```
History类用于存储历史记录和从文件中读取历史记录。它包含两个方法:addRecord和getRecords。addRecord方法用于将记录添加到历史记录中,getRecords方法用于获取历史记录。下面是History类的代码:
```
import java.io.*;
public class History {
private String[] records;
private int currentIndex;
public History() {
records = new String[100];
currentIndex = 0;
}
public void addRecord(String record) {
records[currentIndex] = record;
currentIndex++;
}
public String[] getRecords() {
String[] temp = new String[currentIndex];
for (int i = 0; i < currentIndex; i++) {
temp[i] = records[i];
}
return temp;
}
public void saveToFile() {
try {
PrintWriter writer = new PrintWriter(new FileWriter("history.txt"));
for (int i = 0; i < currentIndex; i++) {
writer.println(records[i]);
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadFromFile() {
try {
BufferedReader reader = new BufferedReader(new FileReader("history.txt"));
String line = reader.readLine();
while (line != null) {
addRecord(line);
line = reader.readLine();
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
Record类用于记录历史记录。它只包含一个字符串变量,用于存储记录。下面是Record类的代码:
```
public class Record {
private String record;
public Record(String record) {
this.record = record;
}
public String getRecord() {
return record;
}
public void setRecord(String record) {
this.record = record;
}
}
```
这就是我们的三个类。现在我们可以编译并运行Calculator类来创建一个标准型计算器,它具有界面功能类似于Windows 7计算器,包括加减乘除,平方,开方等算数运算的基本功能,以及菜单项,用于存储历史记录和从文件中读取历史记录。
阅读全文