From 8f4bb2540d39e6df0ed678e6a039a4620806bbeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BE=8A=E9=A9=BC=E6=83=B3=E7=9D=A1=E8=A7=89?= <204532647@qq.com> Date: Fri, 12 Apr 2024 17:17:42 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=8F=E6=99=93=E9=9D=99=5F4/12=5F=E5=85=B3?= =?UTF-8?q?=E4=BA=8E=E8=AE=A1=E7=AE=97=E5=99=A8=E7=9A=84=E6=96=B0=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E5=AD=98=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/c1.java | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 src/c1.java diff --git a/src/c1.java b/src/c1.java new file mode 100644 index 0000000..167d3a4 --- /dev/null +++ b/src/c1.java @@ -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 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()); + } +}