第二次修改 #2

Merged
fdzcxy212206297 merged 0 commits from master into main 10 months ago

29
.gitignore vendored

@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
.idea/.gitignore vendored

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

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

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

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

@ -1,2 +0,0 @@
# compute

@ -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,23 @@
/**
* ClassName:${NAME}
* Package:
* Description:
* Author Pjj
*
* @Create 2024/10/10 16:14
* @Version 1.0
*///TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
public static void main(String[] args) {
//TIP Press <shortcut actionId="ShowIntentionActions"/> with your caret at the highlighted text
// to see how IntelliJ IDEA suggests fixing it.
System.out.printf("Hello and welcome!");
for (int i = 1; i <= 5; i++) {
//TIP Press <shortcut actionId="Debug"/> to start debugging your code. We have set one <icon src="AllIcons.Debugger.Db_set_breakpoint"/> breakpoint
// for you, but you can always add more by pressing <shortcut actionId="ToggleLineBreakpoint"/>.
System.out.println("i = " + i);
}
}
}

@ -1,6 +1,9 @@
package compute; package compute;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class SimpleCalculator { public class SimpleCalculator {
@ -14,71 +17,71 @@ public class SimpleCalculator {
frame = new JFrame("简单计算器"); frame = new JFrame("简单计算器");
inputField = new JTextField(); inputField = new JTextField();
inputField.setEditable(false); inputField.setEditable(false);
inputField.setFont(new Font("Arial", Font.PLAIN, 24));
inputField.setHorizontalAlignment(JTextField.RIGHT);
// 创建按钮 // 创建按钮
JButton btn1 = new JButton("1"); JButton[] buttons = new JButton[16];
JButton btn2 = new JButton("2"); String[] buttonLabels = {"7", "8", "9", "/",
JButton btn3 = new JButton("3"); "4", "5", "6", "*",
JButton btn4 = new JButton("4"); "1", "2", "3", "-",
JButton btn5 = new JButton("5"); "C", "0", "=", "+"};
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7"); for (int i = 0; i < buttonLabels.length; i++) {
JButton btn8 = new JButton("8"); buttons[i] = new JButton(buttonLabels[i]);
JButton btn9 = new JButton("9"); buttons[i].setFont(new Font("Arial", Font.PLAIN, 24));
JButton btn0 = new JButton("0"); buttons[i].setFocusable(false);
JButton btnAdd = new JButton("+"); buttons[i].setBackground(Color.LIGHT_GRAY);
JButton btnSubtract = new JButton("-"); buttons[i].setBorder(BorderFactory.createLineBorder(Color.GRAY));
JButton btnMultiply = new JButton("*"); final String label = buttonLabels[i];
JButton btnDivide = new JButton("/"); buttons[i].addActionListener(e -> handleButtonClick(label));
JButton btnEqual = new JButton("="); }
JButton btnClear = new JButton("C");
// 设置布局 // 设置布局
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4)); panel.setLayout(new GridLayout(4, 4, 10, 10));
panel.add(btn7); for (JButton button : buttons) {
panel.add(btn8); panel.add(button);
panel.add(btn9); }
panel.add(btnDivide);
panel.add(btn4);
panel.add(btn5);
panel.add(btn6);
panel.add(btnMultiply);
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btnSubtract);
panel.add(btn0);
panel.add(btnEqual);
panel.add(btnClear);
panel.add(btnAdd);
// 添加事件监听器
btn1.addActionListener(e -> appendToInputField("1"));
btn2.addActionListener(e -> appendToInputField("2"));
btn3.addActionListener(e -> appendToInputField("3"));
btn4.addActionListener(e -> appendToInputField("4"));
btn5.addActionListener(e -> appendToInputField("5"));
btn6.addActionListener(e -> appendToInputField("6"));
btn7.addActionListener(e -> appendToInputField("7"));
btn8.addActionListener(e -> appendToInputField("8"));
btn9.addActionListener(e -> appendToInputField("9"));
btn0.addActionListener(e -> appendToInputField("0"));
btnAdd.addActionListener(e -> setOperator("+"));
btnSubtract.addActionListener(e -> setOperator("-"));
btnMultiply.addActionListener(e -> setOperator("*"));
btnDivide.addActionListener(e -> setOperator("/"));
btnEqual.addActionListener(e -> calculateResult());
btnClear.addActionListener(e -> clearInput());
// 设置窗体 // 设置窗体
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400); frame.setSize(400, 400);
frame.setLayout(new BorderLayout());
frame.add(inputField, BorderLayout.NORTH); frame.add(inputField, BorderLayout.NORTH);
frame.add(panel); frame.add(panel, BorderLayout.CENTER);
frame.setVisible(true); frame.setVisible(true);
frame.setLocationRelativeTo(null); // 窗口居中
frame.addKeyListener(new KeyAdapter() { // 支持键盘输入
@Override
public void keyPressed(KeyEvent e) {
char keyChar = e.getKeyChar();
if (Character.isDigit(keyChar) || keyChar == '.' || "+-*/=".indexOf(keyChar) >= 0) {
handleButtonClick(String.valueOf(keyChar));
}
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
clearInput();
}
}
});
}
private void handleButtonClick(String label) {
switch (label) {
case "C":
clearInput();
break;
case "=":
calculateResult();
break;
default:
if ("+-*/".contains(label)) {
setOperator(label);
} else {
appendToInputField(label);
}
break;
}
} }
private void appendToInputField(String value) { private void appendToInputField(String value) {
@ -92,33 +95,39 @@ public class SimpleCalculator {
} }
private void calculateResult() { private void calculateResult() {
num2 = Double.parseDouble(inputField.getText()); try {
double result = 0; num2 = Double.parseDouble(inputField.getText());
double result = 0;
switch (operator) { switch (operator) {
case "+": case "+":
result = num1 + num2; result = num1 + num2;
break; break;
case "-": case "-":
result = num1 - num2; result = num1 - num2;
break; break;
case "*": case "*":
result = num1 * num2; result = num1 * num2;
break; break;
case "/": case "/":
if (num2 != 0) { if (num2 != 0) {
result = num1 / num2; result = num1 / num2;
} else { } else {
JOptionPane.showMessageDialog(frame, "错误:除数不能为零。"); JOptionPane.showMessageDialog(frame, "错误:除数不能为零。");
return; return;
} }
break; break;
}
inputField.setText(String.valueOf(result));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(frame, "无效输入,请检查。");
} }
inputField.setText(String.valueOf(result));
} }
private void clearInput() { private void clearInput() {
inputField.setText(""); inputField.setText("");
num1 = num2 = 0;
operator = "";
} }
public static void main(String[] args) { public static void main(String[] args) {

Loading…
Cancel
Save