|
|
|
@ -2,92 +2,72 @@ import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.datatransfer.Clipboard;
|
|
|
|
|
import java.awt.datatransfer.StringSelection;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
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 CalculatorGUI extends JFrame implements ActionListener {
|
|
|
|
|
private JTextArea displayArea;
|
|
|
|
|
private JTextField inputField;
|
|
|
|
|
private ArrayList<String> history;
|
|
|
|
|
public class CalculatorGUI extends JFrame {
|
|
|
|
|
|
|
|
|
|
private List<String> history;
|
|
|
|
|
private JTextField displayField;
|
|
|
|
|
private JTextArea historyArea;
|
|
|
|
|
|
|
|
|
|
public CalculatorGUI() {
|
|
|
|
|
history = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
// Set up the frame
|
|
|
|
|
setTitle("Calculator");
|
|
|
|
|
setSize(400, 500);
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
setResizable(true);
|
|
|
|
|
setLayout(new BorderLayout());
|
|
|
|
|
|
|
|
|
|
// Display area
|
|
|
|
|
JPanel displayPanel = new JPanel(new GridLayout(1, 2));
|
|
|
|
|
inputField = new JTextField();
|
|
|
|
|
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", "/",
|
|
|
|
|
JPanel buttonPanel = new JPanel(new GridLayout(5, 4));
|
|
|
|
|
String[] buttonLabels = {
|
|
|
|
|
"7", "8", "9", "/",
|
|
|
|
|
"4", "5", "6", "*",
|
|
|
|
|
"1", "2", "3", "-",
|
|
|
|
|
"C", "0", "=", "+"};
|
|
|
|
|
"C", "0", "=", "+",
|
|
|
|
|
"x²", "x³", "√x", "x⁴"
|
|
|
|
|
};
|
|
|
|
|
for (String label : buttonLabels) {
|
|
|
|
|
JButton button = new JButton(label);
|
|
|
|
|
button.addActionListener(this);
|
|
|
|
|
button.addActionListener(new ButtonListener());
|
|
|
|
|
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());
|
|
|
|
|
JButton saveButton = new JButton("Save");
|
|
|
|
|
JButton saveButton = new JButton("Save History");
|
|
|
|
|
saveButton.addActionListener(new SaveListener());
|
|
|
|
|
JButton copyButton = new JButton("Copy");
|
|
|
|
|
JButton copyButton = new JButton("Copy Selection");
|
|
|
|
|
copyButton.addActionListener(new CopyListener());
|
|
|
|
|
JButton clearButton = new JButton("Clear");
|
|
|
|
|
JButton clearButton = new JButton("Clear History");
|
|
|
|
|
clearButton.addActionListener(new ClearListener());
|
|
|
|
|
controlPanel.add(saveButton);
|
|
|
|
|
controlPanel.add(copyButton);
|
|
|
|
|
controlPanel.add(clearButton);
|
|
|
|
|
|
|
|
|
|
add(displayField, BorderLayout.NORTH);
|
|
|
|
|
add(buttonPanel, BorderLayout.CENTER);
|
|
|
|
|
add(scrollPane, BorderLayout.EAST);
|
|
|
|
|
add(controlPanel, BorderLayout.SOUTH);
|
|
|
|
|
|
|
|
|
|
pack();
|
|
|
|
|
setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
String command = e.getActionCommand();
|
|
|
|
|
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 void appendToHistory(String entry) {
|
|
|
|
|
history.add(entry);
|
|
|
|
|
displayHistory();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private double eval(String expression) {
|
|
|
|
@ -161,20 +141,66 @@ public class CalculatorGUI extends JFrame implements ActionListener {
|
|
|
|
|
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 {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
try {
|
|
|
|
|
File file = new File("calculator_history.txt");
|
|
|
|
|
FileWriter writer = new FileWriter(file);
|
|
|
|
|
for (String entry : history) {
|
|
|
|
|
writer.write(entry + "\n");
|
|
|
|
|
JFileChooser fileChooser = new JFileChooser();
|
|
|
|
|
int result = fileChooser.showSaveDialog(null);
|
|
|
|
|
if (result == JFileChooser.APPROVE_OPTION) {
|
|
|
|
|
File file = fileChooser.getSelectedFile();
|
|
|
|
|
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 {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
String selectedText = historyArea.getSelectedText();
|
|
|
|
|
if (selectedText != null) {
|
|
|
|
|
StringSelection selection = new StringSelection(selectedText);
|
|
|
|
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
|
|
clipboard.setContents(selection, null);
|
|
|
|
|
}
|
|
|
|
|
StringSelection selection = new StringSelection(historyArea.getSelectedText());
|
|
|
|
|
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
|
|
|
|
|
clipboard.setContents(selection, null);
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Selection copied to clipboard!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -195,10 +219,11 @@ public class CalculatorGUI extends JFrame implements ActionListener {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
history.clear();
|
|
|
|
|
historyArea.setText("");
|
|
|
|
|
displayHistory();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
new CalculatorGUI();
|
|
|
|
|
}
|
|
|
|
|