From e6a978a1d675cebdc4646e4bb1c17aeb3d76287b Mon Sep 17 00:00:00 2001
From: fdzcxy212206324 <3227658200@qq.com>
Date: Wed, 16 Oct 2024 16:53:21 +0800
Subject: [PATCH 1/7] Initial commit
---
README.md | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 README.md
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e860560
--- /dev/null
+++ b/README.md
@@ -0,0 +1,2 @@
+# new
+
--
2.34.1
From ad3bdfc4793f4743275e6138a4ee7891caa0c5e1 Mon Sep 17 00:00:00 2001
From: zyn <3227658200@qq.com>
Date: Wed, 16 Oct 2024 17:03:14 +0800
Subject: [PATCH 2/7] first
---
Calculator.java | 119 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 119 insertions(+)
create mode 100644 Calculator.java
diff --git a/Calculator.java b/Calculator.java
new file mode 100644
index 0000000..ac5b027
--- /dev/null
+++ b/Calculator.java
@@ -0,0 +1,119 @@
+package java6324.rj;
+
+import javax.swing.*;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+public class Calculator extends JFrame {
+ private JTextField input1, input2, result;
+ private JButton addButton, subtractButton, multiplyButton, divideButton, modButton;
+ private JPanel panel;
+ public Calculator() {
+ setTitle("Simple Calculator");
+ setSize(300, 200);
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ setLayout(new GridLayout(5, 4, 5, 5));
+ panel = new JPanel();
+ panel.setLayout(new GridLayout(5, 4, 5, 5));
+
+ input1 = new JTextField(5);
+ input2 = new JTextField(5);
+ result = new JTextField(5);
+ result.setEditable(false);
+
+ addButton = new JButton("+");
+ subtractButton = new JButton("-");
+ multiplyButton = new JButton("*");
+ divideButton = new JButton("/");
+ modButton = new JButton("%");
+
+ addButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ calculate(1);
+ }
+ });
+
+ subtractButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ calculate(2);
+ }
+ });
+
+ multiplyButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ calculate(3);
+ }
+ });
+
+ divideButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ calculate(4);
+ }
+ });
+
+ modButton.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ calculate(5);
+ }
+ });
+
+ panel.add(new JLabel("Input 1:"));
+ panel.add(input1);
+ panel.add(new JLabel("Input 2:"));
+ panel.add(input2);
+ panel.add(addButton);
+ panel.add(subtractButton);
+ panel.add(multiplyButton);
+ panel.add(divideButton);
+ panel.add(modButton);
+ panel.add(new JLabel());
+ panel.add(result);
+
+ add(panel);
+ setVisible(true);
+ }
+
+ private void calculate(int operation) {
+ try {
+ double num1 = Double.parseDouble(input1.getText());
+ double num2 = Double.parseDouble(input2.getText());
+ double res = 0;
+
+ switch (operation) {
+ case 1:
+ res = num1 + num2;
+ break;
+ case 2:
+ res = num1 - num2;
+ break;
+ case 3:
+ res = num1 * num2;
+ break;
+ case 4:
+ if (num2 != 0) {
+ res = num1 / num2;
+ } else {
+ result.setText("Error");
+ return;
+ }
+ break;
+ case 5:
+ if (num2 != 0) {
+ res = num1 % num2;
+ } else {
+ result.setText("Error");
+ return;
+ }
+ break;
+ }
+ result.setText(String.valueOf(res));
+ } catch (NumberFormatException e) {
+ result.setText("Invalid Input");
+ }
+ }
+
+ public static void main(String[] args) {
+ new Calculator();
+ }
+}
--
2.34.1
From d1ead7bbb96e0a88cc8f1f427688178e8c2d7fe5 Mon Sep 17 00:00:00 2001
From: liwenjun <2684903586@qq.com>
Date: Wed, 16 Oct 2024 19:45:00 +0800
Subject: [PATCH 3/7] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E4=BF=AE?=
=?UTF-8?q?=E6=94=B9=EF=BC=9A=E8=AE=A1=E7=AE=97=E5=99=A8=E6=A0=B7=E5=BC=8F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/.gitignore | 3 +
.idea/misc.xml | 6 ++
.idea/modules.xml | 9 +++
.idea/new.iml | 9 +++
.idea/vcs.xml | 6 ++
Calculator.java | 119 ---------------------------------
Calculator/Calculator.iml | 11 +++
Calculator/src/Calculator.java | 110 ++++++++++++++++++++++++++++++
8 files changed, 154 insertions(+), 119 deletions(-)
create mode 100644 .idea/.gitignore
create mode 100644 .idea/misc.xml
create mode 100644 .idea/modules.xml
create mode 100644 .idea/new.iml
create mode 100644 .idea/vcs.xml
delete mode 100644 Calculator.java
create mode 100644 Calculator/Calculator.iml
create mode 100644 Calculator/src/Calculator.java
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.java b/Calculator.java
deleted file mode 100644
index ac5b027..0000000
--- a/Calculator.java
+++ /dev/null
@@ -1,119 +0,0 @@
-package java6324.rj;
-
-import javax.swing.*;
-import java.awt.*;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
-
-public class Calculator extends JFrame {
- private JTextField input1, input2, result;
- private JButton addButton, subtractButton, multiplyButton, divideButton, modButton;
- private JPanel panel;
- public Calculator() {
- setTitle("Simple Calculator");
- setSize(300, 200);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new GridLayout(5, 4, 5, 5));
- panel = new JPanel();
- panel.setLayout(new GridLayout(5, 4, 5, 5));
-
- input1 = new JTextField(5);
- input2 = new JTextField(5);
- result = new JTextField(5);
- result.setEditable(false);
-
- addButton = new JButton("+");
- subtractButton = new JButton("-");
- multiplyButton = new JButton("*");
- divideButton = new JButton("/");
- modButton = new JButton("%");
-
- addButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- calculate(1);
- }
- });
-
- subtractButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- calculate(2);
- }
- });
-
- multiplyButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- calculate(3);
- }
- });
-
- divideButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- calculate(4);
- }
- });
-
- modButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- calculate(5);
- }
- });
-
- panel.add(new JLabel("Input 1:"));
- panel.add(input1);
- panel.add(new JLabel("Input 2:"));
- panel.add(input2);
- panel.add(addButton);
- panel.add(subtractButton);
- panel.add(multiplyButton);
- panel.add(divideButton);
- panel.add(modButton);
- panel.add(new JLabel());
- panel.add(result);
-
- add(panel);
- setVisible(true);
- }
-
- private void calculate(int operation) {
- try {
- double num1 = Double.parseDouble(input1.getText());
- double num2 = Double.parseDouble(input2.getText());
- double res = 0;
-
- switch (operation) {
- case 1:
- res = num1 + num2;
- break;
- case 2:
- res = num1 - num2;
- break;
- case 3:
- res = num1 * num2;
- break;
- case 4:
- if (num2 != 0) {
- res = num1 / num2;
- } else {
- result.setText("Error");
- return;
- }
- break;
- case 5:
- if (num2 != 0) {
- res = num1 % num2;
- } else {
- result.setText("Error");
- return;
- }
- break;
- }
- result.setText(String.valueOf(res));
- } catch (NumberFormatException e) {
- result.setText("Invalid Input");
- }
- }
-
- public static void main(String[] args) {
- new Calculator();
- }
-}
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..1a26029
--- /dev/null
+++ b/Calculator/src/Calculator.java
@@ -0,0 +1,110 @@
+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, 24)); // 增加文本框字体大小
+ add(textField, BorderLayout.NORTH);
+
+ JPanel panel = new JPanel();
+ panel.setLayout(new GridLayout(5, 4, 10, 10)); // 增加网格布局的间距
+
+ String[] buttons = {
+ "7", "8", "9", "/",
+ "4", "5", "6", "*",
+ "1", "2", "3", "-",
+ "C", "0", ".", "+",
+ "="
+ };
+
+ for (String text : buttons) {
+ JButton button = new JButton(text);
+ button.setFont(new Font("Arial", Font.BOLD, 18)); // 增加按钮字体大小
+ button.addActionListener(this);
+ panel.add(button);
+ }
+
+ add(panel, BorderLayout.CENTER);
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ setSize(800, 500); // 设置窗口大小为宽800像素,高500像素
+ setLocationRelativeTo(null); // 窗口居中显示
+ pack();
+ 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;
+ }
+ }
+ }
+
+ 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");
+ return;
+ }
+ break;
+ }
+ textField.setText("" + result);
+ operator = "";
+ calculating = true;
+ }
+
+ public static void main(String[] args) {
+ new Calculator();
+ }
+}
\ No newline at end of file
--
2.34.1
From 225befaf5184594293fc073970c97a93e7928919 Mon Sep 17 00:00:00 2001
From: liwenjun <2684903586@qq.com>
Date: Thu, 17 Oct 2024 16:12:04 +0800
Subject: [PATCH 4/7] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BF=AE?=
=?UTF-8?q?=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Calculator/src/Calculator.java | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/Calculator/src/Calculator.java b/Calculator/src/Calculator.java
index 1a26029..b89831e 100644
--- a/Calculator/src/Calculator.java
+++ b/Calculator/src/Calculator.java
@@ -37,7 +37,7 @@ public class Calculator extends JFrame implements ActionListener {
add(panel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(800, 500); // 设置窗口大小为宽800像素,高500像素
+ setSize(1000, 600); // 设置窗口大小为宽800像素,高500像素
setLocationRelativeTo(null); // 窗口居中显示
pack();
setVisible(true);
@@ -99,7 +99,11 @@ public class Calculator extends JFrame implements ActionListener {
}
break;
}
- textField.setText("" + result);
+ if (result == Math.floor(result)) {
+ textField.setText(String.valueOf((int) result));
+ } else {
+ textField.setText(String.valueOf(result));
+ }
operator = "";
calculating = true;
}
--
2.34.1
From 981f4a0737687b552940fd5f44c5dcd565cc9c02 Mon Sep 17 00:00:00 2001
From: zyn <3227658200@qq.com>
Date: Thu, 17 Oct 2024 16:27:35 +0800
Subject: [PATCH 5/7] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E6=AC=A1=E4=BF=AE?=
=?UTF-8?q?=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Calculator/src/Calculator.java | 32 +++++++++++++++++++++++---------
1 file changed, 23 insertions(+), 9 deletions(-)
diff --git a/Calculator/src/Calculator.java b/Calculator/src/Calculator.java
index b89831e..e2f067f 100644
--- a/Calculator/src/Calculator.java
+++ b/Calculator/src/Calculator.java
@@ -18,14 +18,14 @@ public class Calculator extends JFrame implements ActionListener {
add(textField, BorderLayout.NORTH);
JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(5, 4, 10, 10)); // 增加网格布局的间距
+ panel.setLayout(new GridLayout(6, 4, 10, 10)); // 修改为6行以容纳根号按钮
String[] buttons = {
- "7", "8", "9", "/",
- "4", "5", "6", "*",
+ "7", "8", "9", "÷",
+ "4", "5", "6", "×",
"1", "2", "3", "-",
"C", "0", ".", "+",
- "="
+ "√", "=", ""
};
for (String text : buttons) {
@@ -37,7 +37,7 @@ public class Calculator extends JFrame implements ActionListener {
add(panel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(1000, 600); // 设置窗口大小为宽800像素,高500像素
+ setSize(400, 600); // 修改窗口大小为宽400像素,高600像素以适应新布局
setLocationRelativeTo(null); // 窗口居中显示
pack();
setVisible(true);
@@ -62,8 +62,8 @@ public class Calculator extends JFrame implements ActionListener {
break;
case "+":
case "-":
- case "*":
- case "/":
+ case "×":
+ case "÷":
if (!calculating) {
result = Double.parseDouble(textField.getText());
operator = command;
@@ -75,6 +75,9 @@ public class Calculator extends JFrame implements ActionListener {
calculate(Double.parseDouble(textField.getText()));
}
break;
+ case "√":
+ calculateMathFunction();
+ break;
}
}
}
@@ -87,10 +90,10 @@ public class Calculator extends JFrame implements ActionListener {
case "-":
result -= n;
break;
- case "*":
+ case "×":
result *= n;
break;
- case "/":
+ case "÷":
if (n != 0) {
result /= n;
} else {
@@ -108,6 +111,17 @@ public class Calculator extends JFrame implements ActionListener {
calculating = true;
}
+ private void calculateMathFunction() {
+ try {
+ double num = Double.parseDouble(textField.getText());
+ result = Math.sqrt(num);
+ textField.setText(String.valueOf(result));
+ } catch (NumberFormatException ex) {
+ JOptionPane.showMessageDialog(this, "请输入一个有效的数字");
+ }
+ calculating = true;
+ }
+
public static void main(String[] args) {
new Calculator();
}
--
2.34.1
From 45a0084ec9ae02c49bf4945a33cc95aa91b5f369 Mon Sep 17 00:00:00 2001
From: liwenjun <2684903586@qq.com>
Date: Thu, 17 Oct 2024 16:42:53 +0800
Subject: [PATCH 6/7] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1=E4=BF=AE?=
=?UTF-8?q?=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Calculator/src/Calculator.java | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/Calculator/src/Calculator.java b/Calculator/src/Calculator.java
index e2f067f..8e492e6 100644
--- a/Calculator/src/Calculator.java
+++ b/Calculator/src/Calculator.java
@@ -18,7 +18,7 @@ public class Calculator extends JFrame implements ActionListener {
add(textField, BorderLayout.NORTH);
JPanel panel = new JPanel();
- panel.setLayout(new GridLayout(6, 4, 10, 10)); // 修改为6行以容纳根号按钮
+ panel.setLayout(new GridLayout(5, 4, 10, 10)); // 修改为5行以适应新布局
String[] buttons = {
"7", "8", "9", "÷",
@@ -29,10 +29,12 @@ public class Calculator extends JFrame implements ActionListener {
};
for (String text : buttons) {
- JButton button = new JButton(text);
- button.setFont(new Font("Arial", Font.BOLD, 18)); // 增加按钮字体大小
- button.addActionListener(this);
- panel.add(button);
+ if (!text.isEmpty()) { // 确保不创建空按钮
+ JButton button = new JButton(text);
+ button.setFont(new Font("Arial", Font.BOLD, 18)); // 增加按钮字体大小
+ button.addActionListener(this);
+ panel.add(button);
+ }
}
add(panel, BorderLayout.CENTER);
@@ -115,7 +117,11 @@ public class Calculator extends JFrame implements ActionListener {
try {
double num = Double.parseDouble(textField.getText());
result = Math.sqrt(num);
- textField.setText(String.valueOf(result));
+ if (result == Math.floor(result)) { // 检查结果是否已经是整数
+ textField.setText(String.valueOf((int) result)); // 转换为整数并显示
+ } else {
+ textField.setText(String.valueOf(result)); // 否则显示浮点数
+ }
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(this, "请输入一个有效的数字");
}
--
2.34.1
From 61f7f5457d1af2b70ba6389a8945d1d0bc7bda57 Mon Sep 17 00:00:00 2001
From: zyn <3227658200@qq.com>
Date: Thu, 17 Oct 2024 17:00:21 +0800
Subject: [PATCH 7/7] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E4=BF=AE?=
=?UTF-8?q?=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Calculator/src/Calculator.java | 37 ++++++++++++++++++++++------------
1 file changed, 24 insertions(+), 13 deletions(-)
diff --git a/Calculator/src/Calculator.java b/Calculator/src/Calculator.java
index 8e492e6..f701387 100644
--- a/Calculator/src/Calculator.java
+++ b/Calculator/src/Calculator.java
@@ -11,14 +11,20 @@ public class Calculator extends JFrame implements ActionListener {
public Calculator() {
setTitle("计算器");
+
+ // 设置文本字段样式
textField = new JTextField("0");
textField.setEditable(false);
- textField.setHorizontalAlignment(JTextField.RIGHT); // 右对齐显示
- textField.setFont(new Font("Arial", Font.BOLD, 24)); // 增加文本框字体大小
+ 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)); // 修改为5行以适应新布局
+ panel.setLayout(new GridLayout(5, 4, 10, 10));
+ panel.setBackground(new Color(220, 220, 220)); // 设置面板背景颜色
String[] buttons = {
"7", "8", "9", "÷",
@@ -29,19 +35,24 @@ public class Calculator extends JFrame implements ActionListener {
};
for (String text : buttons) {
- if (!text.isEmpty()) { // 确保不创建空按钮
+ if (!text.isEmpty()) {
JButton button = new JButton(text);
- button.setFont(new Font("Arial", Font.BOLD, 18)); // 增加按钮字体大小
+ 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(400, 600); // 修改窗口大小为宽400像素,高600像素以适应新布局
- setLocationRelativeTo(null); // 窗口居中显示
- pack();
+ setSize(420, 640); // 稍微增大窗口以适应新的布局和样式
+ setLocationRelativeTo(null);
setVisible(true);
}
@@ -99,7 +110,7 @@ public class Calculator extends JFrame implements ActionListener {
if (n != 0) {
result /= n;
} else {
- JOptionPane.showMessageDialog(this, "除数不能为0");
+ JOptionPane.showMessageDialog(this, "除数不能为0", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
break;
@@ -117,13 +128,13 @@ public class Calculator extends JFrame implements ActionListener {
try {
double num = Double.parseDouble(textField.getText());
result = Math.sqrt(num);
- if (result == Math.floor(result)) { // 检查结果是否已经是整数
- textField.setText(String.valueOf((int) result)); // 转换为整数并显示
+ if (result == Math.floor(result)) {
+ textField.setText(String.valueOf((int) result));
} else {
- textField.setText(String.valueOf(result)); // 否则显示浮点数
+ textField.setText(String.valueOf(result));
}
} catch (NumberFormatException ex) {
- JOptionPane.showMessageDialog(this, "请输入一个有效的数字");
+ JOptionPane.showMessageDialog(this, "请输入一个有效的数字", "错误", JOptionPane.ERROR_MESSAGE);
}
calculating = true;
}
--
2.34.1