夏晓静4/13代码更新

main
羊驼想睡觉 1 year ago
parent 5349ad99c0
commit 7af3528d9e

@ -2,92 +2,72 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection; import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent; import java.awt.event.*;
import java.awt.event.ActionListener;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
public class CalculatorGUI extends JFrame implements ActionListener { public class CalculatorGUI extends JFrame {
private JTextArea displayArea;
private JTextField inputField; private List<String> history;
private ArrayList<String> history; private JTextField displayField;
private JTextArea historyArea; private JTextArea historyArea;
public CalculatorGUI() { public CalculatorGUI() {
history = new ArrayList<>(); history = new ArrayList<>();
// Set up the frame
setTitle("Calculator"); setTitle("Calculator");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(true);
setLayout(new BorderLayout()); setLayout(new BorderLayout());
// Display area JPanel buttonPanel = new JPanel(new GridLayout(5, 4));
JPanel displayPanel = new JPanel(new GridLayout(1, 2)); String[] buttonLabels = {
inputField = new JTextField(); "7", "8", "9", "/",
inputField.setEditable(false);
displayPanel.add(inputField);
historyArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(historyArea);
displayPanel.add(scrollPane);
add(displayPanel, BorderLayout.NORTH);
// Buttons
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
String[] buttonLabels = {"7", "8", "9", "/",
"4", "5", "6", "*", "4", "5", "6", "*",
"1", "2", "3", "-", "1", "2", "3", "-",
"C", "0", "=", "+"}; "C", "0", "=", "+",
"x²", "x³", "√x", "x⁴"
};
for (String label : buttonLabels) { for (String label : buttonLabels) {
JButton button = new JButton(label); JButton button = new JButton(label);
button.addActionListener(this); button.addActionListener(new ButtonListener());
buttonPanel.add(button); buttonPanel.add(button);
} }
add(buttonPanel, BorderLayout.CENTER);
// Save, Copy, Clear buttons displayField = new JTextField(20);
displayField.setEditable(false);
displayField.setHorizontalAlignment(SwingConstants.RIGHT);
historyArea = new JTextArea(10, 20);
historyArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(historyArea);
JPanel controlPanel = new JPanel(new FlowLayout()); JPanel controlPanel = new JPanel(new FlowLayout());
JButton saveButton = new JButton("Save"); JButton saveButton = new JButton("Save History");
saveButton.addActionListener(new SaveListener()); saveButton.addActionListener(new SaveListener());
JButton copyButton = new JButton("Copy"); JButton copyButton = new JButton("Copy Selection");
copyButton.addActionListener(new CopyListener()); copyButton.addActionListener(new CopyListener());
JButton clearButton = new JButton("Clear"); JButton clearButton = new JButton("Clear History");
clearButton.addActionListener(new ClearListener()); clearButton.addActionListener(new ClearListener());
controlPanel.add(saveButton); controlPanel.add(saveButton);
controlPanel.add(copyButton); controlPanel.add(copyButton);
controlPanel.add(clearButton); controlPanel.add(clearButton);
add(displayField, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
add(scrollPane, BorderLayout.EAST);
add(controlPanel, BorderLayout.SOUTH); add(controlPanel, BorderLayout.SOUTH);
pack();
setVisible(true); setVisible(true);
} }
@Override private void appendToHistory(String entry) {
public void actionPerformed(ActionEvent e) { history.add(entry);
String command = e.getActionCommand(); displayHistory();
switch (command) {
case "C":
inputField.setText("");
break;
case "=":
calculate();
break;
default:
inputField.setText(inputField.getText() + command);
}
}
private void calculate() {
String expression = inputField.getText();
try {
String result = String.valueOf(eval(expression));
history.add(expression + " = " + result);
displayHistory();
inputField.setText(result);
} catch (NumberFormatException | ArithmeticException e) {
inputField.setText("Error");
}
} }
private double eval(String expression) { private double eval(String expression) {
@ -161,20 +141,66 @@ public class CalculatorGUI extends JFrame implements ActionListener {
historyArea.setText(sb.toString()); historyArea.setText(sb.toString());
} }
private class ButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (!command.equals("=")) {
if (command.equals("x²") || command.equals("x³") || command.equals("√x") || command.equals("x⁴")) {
// 计算函数值
String expression = displayField.getText();
double result = calculateFunction(expression, command);
displayField.setText(Double.toString(result));
} else {
displayField.setText(displayField.getText() + command);
}
} else {
// Evaluate expression
String expression = displayField.getText();
try {
double result = eval(expression);
appendToHistory(expression + " = " + result);
displayField.setText(Double.toString(result));
} catch (Exception ex) {
appendToHistory("Error: " + ex.getMessage());
}
}
}
}
private double calculateFunction(String expression, String function) {
// 根据不同的函数类型计算结果
double value = Double.parseDouble(expression);
switch (function) {
case "x²":
return value * value;
case "x³":
return value * value * value;
case "√x":
return Math.sqrt(value);
case "x⁴":
return value * value * value * value;
default:
return 0;
}
}
private class SaveListener implements ActionListener { private class SaveListener implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { JFileChooser fileChooser = new JFileChooser();
File file = new File("calculator_history.txt"); int result = fileChooser.showSaveDialog(null);
FileWriter writer = new FileWriter(file); if (result == JFileChooser.APPROVE_OPTION) {
for (String entry : history) { File file = fileChooser.getSelectedFile();
writer.write(entry + "\n"); try (FileWriter writer = new FileWriter(file)) {
for (String entry : history) {
writer.write(entry + "\n");
}
writer.flush();
JOptionPane.showMessageDialog(null, "History saved successfully!");
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Error saving history: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} }
writer.close();
JOptionPane.showMessageDialog(CalculatorGUI.this, "History saved successfully");
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(CalculatorGUI.this, "Error saving history");
} }
} }
} }
@ -182,12 +208,10 @@ public class CalculatorGUI extends JFrame implements ActionListener {
private class CopyListener implements ActionListener { private class CopyListener implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String selectedText = historyArea.getSelectedText(); StringSelection selection = new StringSelection(historyArea.getSelectedText());
if (selectedText != null) { Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection selection = new StringSelection(selectedText); clipboard.setContents(selection, null);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); JOptionPane.showMessageDialog(null, "Selection copied to clipboard!");
clipboard.setContents(selection, null);
}
} }
} }
@ -195,10 +219,11 @@ public class CalculatorGUI extends JFrame implements ActionListener {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
history.clear(); history.clear();
historyArea.setText(""); displayHistory();
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
new CalculatorGUI(); new CalculatorGUI();
} }

Loading…
Cancel
Save