夏晓静_4/12_关于计算器的新代码存档

pull/1/head
羊驼想睡觉 1 year ago
parent fce2a2f13b
commit 8f4bb2540d

@ -0,0 +1,167 @@
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class C1 extends JFrame implements ActionListener {
private JTextField displayField;
private JTextArea historyArea;
private List<String> historyList;
private double num1, num2;
private String operator;
public C1() {
super("Calculator");
historyList = new ArrayList<>();
// 设置布局
setLayout(new BorderLayout());
// 显示区域
JPanel displayPanel = new JPanel(new GridLayout(1, 2));
displayField = new JTextField();
displayField.setEditable(false);
historyArea = new JTextArea();
historyArea.setEditable(false);
JScrollPane historyScrollPane = new JScrollPane(historyArea);
displayPanel.add(displayField);
displayPanel.add(historyScrollPane);
add(displayPanel, BorderLayout.NORTH);
// 按钮区域
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
String[] buttonLabels = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
for (String label : buttonLabels) {
JButton button = new JButton(label);
button.addActionListener(this);
buttonPanel.add(button);
}
JButton saveButton = new JButton("保存");
saveButton.addActionListener(this);
JButton copyButton = new JButton("复制");
copyButton.addActionListener(this);
JButton clearButton = new JButton("清除");
clearButton.addActionListener(this);
buttonPanel.add(saveButton);
buttonPanel.add(copyButton);
buttonPanel.add(clearButton);
add(buttonPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 400);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("保存")) {
saveHistoryToFile();
} else if (command.equals("复制")) {
copyToClipboard();
} else if (command.equals("清除")) {
clearHistory();
} else if (Character.isDigit(command.charAt(0)) || command.equals(".")) {
displayField.setText(displayField.getText() + command);
} else if (command.equals("=")) {
calculate();
} else {
// 运算符
num1 = Double.parseDouble(displayField.getText());
operator = command;
displayField.setText("");
}
}
private void calculate() {
num2 = Double.parseDouble(displayField.getText());
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 != 0) {
result = num1 / num2;
} else {
displayField.setText("Error");
return;
}
break;
}
displayField.setText(Double.toString(result));
String historyEntry = num1 + " " + operator + " " + num2 + " = " + result;
historyList.add(historyEntry);
updateHistoryArea();
}
private void updateHistoryArea() {
historyArea.setText("");
for (String entry : historyList) {
historyArea.append(entry + "\n");
}
}
private void saveHistoryToFile() {
JFileChooser fileChooser = new JFileChooser();
int result = fileChooser.showSaveDialog(this);
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (FileWriter writer = new FileWriter(file)) {
for (String entry : historyList) {
writer.write(entry + "\n");
}
writer.flush();
JOptionPane.showMessageDialog(this, "保存成功");
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "保存失败: " + e.getMessage());
}
}
}
private void copyToClipboard() {
String selectedText = historyArea.getSelectedText();
if (selectedText != null && !selectedText.isEmpty()) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(selectedText), null);
}
}
private void clearHistory() {
historyList.clear();
updateHistoryArea();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new C1());
}
}
Loading…
Cancel
Save