aaa #1

Open
fdzcxy212206324 wants to merge 0 commits from master into main

3
.idea/.gitignore vendored

@ -0,0 +1,3 @@
# 默认忽略的文件
/shelf/
/workspace.xml

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Calculator/Calculator.iml" filepath="$PROJECT_DIR$/Calculator/Calculator.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/new.iml" filepath="$PROJECT_DIR$/.idea/new.iml" />
</modules>
</component>
</project>

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

@ -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();
}
}

@ -1,2 +0,0 @@
# new
Loading…
Cancel
Save