diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..065cbba --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/new.iml b/.idea/new.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/new.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Calculator/Calculator.iml b/Calculator/Calculator.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/Calculator/Calculator.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Calculator/src/Calculator.java b/Calculator/src/Calculator.java new file mode 100644 index 0000000..f701387 --- /dev/null +++ b/Calculator/src/Calculator.java @@ -0,0 +1,145 @@ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class Calculator extends JFrame implements ActionListener { + private final JTextField textField; // 显示输入和结果的文本框 + private double result = 0; // 计算结果 + private String operator = ""; // 运算符 + private boolean calculating = true; // 是否正在计算中 + + public Calculator() { + setTitle("计算器"); + + // 设置文本字段样式 + textField = new JTextField("0"); + textField.setEditable(false); + textField.setHorizontalAlignment(JTextField.RIGHT); + textField.setFont(new Font("Arial", Font.BOLD, 28)); + textField.setBackground(new Color(240, 240, 240)); // 设置背景颜色 + textField.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 设置边框 + + add(textField, BorderLayout.NORTH); + + JPanel panel = new JPanel(); + panel.setLayout(new GridLayout(5, 4, 10, 10)); + panel.setBackground(new Color(220, 220, 220)); // 设置面板背景颜色 + + String[] buttons = { + "7", "8", "9", "÷", + "4", "5", "6", "×", + "1", "2", "3", "-", + "C", "0", ".", "+", + "√", "=", "" + }; + + for (String text : buttons) { + if (!text.isEmpty()) { + JButton button = new JButton(text); + button.setFont(new Font("Arial", Font.BOLD, 22)); + button.setFocusPainted(false); // 去掉按钮焦点时的边框 + button.setBackground(new Color(180, 180, 180)); // 设置按钮背景颜色 + button.setForeground(Color.BLACK); // 设置按钮文字颜色 + button.setBorder(BorderFactory.createLineBorder(Color.BLACK, 2)); // 设置按钮边框 + button.addActionListener(this); + panel.add(button); + } + } + + add(panel, BorderLayout.CENTER); + + // 设置窗口属性 + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setSize(420, 640); // 稍微增大窗口以适应新的布局和样式 + setLocationRelativeTo(null); + setVisible(true); + } + + public void actionPerformed(ActionEvent e) { + String command = e.getActionCommand(); + if ('0' <= command.charAt(0) && command.charAt(0) <= '9' || command.equals(".")) { + if (calculating) { + textField.setText(command); + calculating = false; + } else { + textField.setText(textField.getText() + command); + } + } else { + switch (command) { + case "C": + textField.setText("0"); + operator = ""; + result = 0; + calculating = true; + break; + case "+": + case "-": + case "×": + case "÷": + if (!calculating) { + result = Double.parseDouble(textField.getText()); + operator = command; + calculating = true; + } + break; + case "=": + if (!calculating) { + calculate(Double.parseDouble(textField.getText())); + } + break; + case "√": + calculateMathFunction(); + break; + } + } + } + + private void calculate(double n) { + switch (operator) { + case "+": + result += n; + break; + case "-": + result -= n; + break; + case "×": + result *= n; + break; + case "÷": + if (n != 0) { + result /= n; + } else { + JOptionPane.showMessageDialog(this, "除数不能为0", "错误", JOptionPane.ERROR_MESSAGE); + return; + } + break; + } + if (result == Math.floor(result)) { + textField.setText(String.valueOf((int) result)); + } else { + textField.setText(String.valueOf(result)); + } + operator = ""; + calculating = true; + } + + private void calculateMathFunction() { + try { + double num = Double.parseDouble(textField.getText()); + result = Math.sqrt(num); + if (result == Math.floor(result)) { + textField.setText(String.valueOf((int) result)); + } else { + textField.setText(String.valueOf(result)); + } + } catch (NumberFormatException ex) { + JOptionPane.showMessageDialog(this, "请输入一个有效的数字", "错误", JOptionPane.ERROR_MESSAGE); + } + calculating = true; + } + + public static void main(String[] args) { + new Calculator(); + } +} \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 100644 index e860560..0000000 --- a/README.md +++ /dev/null @@ -1,2 +0,0 @@ -# new -