From 9bb86a0c01dec2a8036392decd1a790f7b5de236 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=91=E7=AB=8B?= <11030279+s1059354356@user.noreply.gitee.com> Date: Tue, 24 May 2022 15:36:07 +0000 Subject: [PATCH 1/6] add LICENSE. --- LICENSE | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ee58399 --- /dev/null +++ b/LICENSE @@ -0,0 +1,127 @@ + 木兰宽松许可证, 第2版 + + 木兰宽松许可证, 第2版 + 2020年1月 http://license.coscl.org.cn/MulanPSL2 + + + 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束: + + 0. 定义 + + “软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 + + “贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 + + “贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 + + “法人实体”是指提交贡献的机构及其“关联实体”。 + + “关联实体”是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 + + 1. 授予版权许可 + + 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。 + + 2. 授予专利许可 + + 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。 + + 3. 无商标许可 + + “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。 + + 4. 分发限制 + + 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 + + 5. 免责声明与责任限制 + + “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。 + + 6. 语言 + “本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。 + + 条款结束 + + 如何将木兰宽松许可证,第2版,应用到您的软件 + + 如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步: + + 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字; + + 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中; + + 3, 请将如下声明文本放入每个源文件的头部注释中。 + + Copyright (c) [Year] [name of copyright holder] + [Software Name] is licensed under Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. + + + Mulan Permissive Software License,Version 2 + + Mulan Permissive Software License,Version 2 (Mulan PSL v2) + January 2020 http://license.coscl.org.cn/MulanPSL2 + + Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions: + + 0. Definition + + Software means the program and related documents which are licensed under this License and comprise all Contribution(s). + + Contribution means the copyrightable work licensed by a particular Contributor under this License. + + Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. + + Legal Entity means the entity making a Contribution and all its Affiliates. + + Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. + + 1. Grant of Copyright License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. + + 2. Grant of Patent License + + Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. + + 3. No Trademark License + + No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4. + + 4. Distribution Restriction + + You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. + + 5. Disclaimer of Warranty and Limitation of Liability + + THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + + 6. Language + + THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL. + + END OF THE TERMS AND CONDITIONS + + How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software + + To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps: + + i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; + + ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package; + + iii Attach the statement to the appropriate annotated syntax at the beginning of each source file. + + + Copyright (c) [Year] [name of copyright holder] + [Software Name] is licensed under Mulan PSL v2. + You can use this software according to the terms and conditions of the Mulan PSL v2. + You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 + THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + See the Mulan PSL v2 for more details. -- Gitee From f9679ba999f57d0f4d55e9d3ac401c8e93238f5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=91=E7=AB=8B?= <11030279+s1059354356@user.noreply.gitee.com> Date: Wed, 25 May 2022 13:50:44 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20LICE?= =?UTF-8?q?NSE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LICENSE | 127 -------------------------------------------------------- 1 file changed, 127 deletions(-) delete mode 100644 LICENSE diff --git a/LICENSE b/LICENSE deleted file mode 100644 index ee58399..0000000 --- a/LICENSE +++ /dev/null @@ -1,127 +0,0 @@ - 木兰宽松许可证, 第2版 - - 木兰宽松许可证, 第2版 - 2020年1月 http://license.coscl.org.cn/MulanPSL2 - - - 您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束: - - 0. 定义 - - “软件”是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 - - “贡献”是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 - - “贡献者”是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 - - “法人实体”是指提交贡献的机构及其“关联实体”。 - - “关联实体”是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 - - 1. 授予版权许可 - - 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可以复制、使用、修改、分发其“贡献”,不论修改与否。 - - 2. 授予专利许可 - - 每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权行动之日终止。 - - 3. 无商标许可 - - “本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定的声明义务而必须使用除外。 - - 4. 分发限制 - - 您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 - - 5. 免责声明与责任限制 - - “软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于何种法律理论,即使其曾被建议有此种损失的可能性。 - - 6. 语言 - “本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文版为准。 - - 条款结束 - - 如何将木兰宽松许可证,第2版,应用到您的软件 - - 如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步: - - 1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字; - - 2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中; - - 3, 请将如下声明文本放入每个源文件的头部注释中。 - - Copyright (c) [Year] [name of copyright holder] - [Software Name] is licensed under Mulan PSL v2. - You can use this software according to the terms and conditions of the Mulan PSL v2. - You may obtain a copy of Mulan PSL v2 at: - http://license.coscl.org.cn/MulanPSL2 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - See the Mulan PSL v2 for more details. - - - Mulan Permissive Software License,Version 2 - - Mulan Permissive Software License,Version 2 (Mulan PSL v2) - January 2020 http://license.coscl.org.cn/MulanPSL2 - - Your reproduction, use, modification and distribution of the Software shall be subject to Mulan PSL v2 (this License) with the following terms and conditions: - - 0. Definition - - Software means the program and related documents which are licensed under this License and comprise all Contribution(s). - - Contribution means the copyrightable work licensed by a particular Contributor under this License. - - Contributor means the Individual or Legal Entity who licenses its copyrightable work under this License. - - Legal Entity means the entity making a Contribution and all its Affiliates. - - Affiliates means entities that control, are controlled by, or are under common control with the acting entity under this License, ‘control’ means direct or indirect ownership of at least fifty percent (50%) of the voting power, capital or other securities of controlled or commonly controlled entity. - - 1. Grant of Copyright License - - Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable copyright license to reproduce, use, modify, or distribute its Contribution, with modification or not. - - 2. Grant of Patent License - - Subject to the terms and conditions of this License, each Contributor hereby grants to you a perpetual, worldwide, royalty-free, non-exclusive, irrevocable (except for revocation under this Section) patent license to make, have made, use, offer for sale, sell, import or otherwise transfer its Contribution, where such patent license is only limited to the patent claims owned or controlled by such Contributor now or in future which will be necessarily infringed by its Contribution alone, or by combination of the Contribution with the Software to which the Contribution was contributed. The patent license shall not apply to any modification of the Contribution, and any other combination which includes the Contribution. If you or your Affiliates directly or indirectly institute patent litigation (including a cross claim or counterclaim in a litigation) or other patent enforcement activities against any individual or entity by alleging that the Software or any Contribution in it infringes patents, then any patent license granted to you under this License for the Software shall terminate as of the date such litigation or activity is filed or taken. - - 3. No Trademark License - - No trademark license is granted to use the trade names, trademarks, service marks, or product names of Contributor, except as required to fulfill notice requirements in Section 4. - - 4. Distribution Restriction - - You may distribute the Software in any medium with or without modification, whether in source or executable forms, provided that you provide recipients with a copy of this License and retain copyright, patent, trademark and disclaimer statements in the Software. - - 5. Disclaimer of Warranty and Limitation of Liability - - THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. - - 6. Language - - THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION SHALL PREVAIL. - - END OF THE TERMS AND CONDITIONS - - How to Apply the Mulan Permissive Software License,Version 2 (Mulan PSL v2) to Your Software - - To apply the Mulan PSL v2 to your work, for easy identification by recipients, you are suggested to complete following three steps: - - i Fill in the blanks in following statement, including insert your software name, the year of the first publication of your software, and your name identified as the copyright owner; - - ii Create a file named “LICENSE” which contains the whole context of this License in the first directory of your software package; - - iii Attach the statement to the appropriate annotated syntax at the beginning of each source file. - - - Copyright (c) [Year] [name of copyright holder] - [Software Name] is licensed under Mulan PSL v2. - You can use this software according to the terms and conditions of the Mulan PSL v2. - You may obtain a copy of Mulan PSL v2 at: - http://license.coscl.org.cn/MulanPSL2 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - See the Mulan PSL v2 for more details. -- Gitee From 2184ec3ad2ba7887e2587a0282d23991b1c4cb22 Mon Sep 17 00:00:00 2001 From: 10593 <10593@LAPTOP-02BTTG0N> Date: Wed, 25 May 2022 22:40:38 +0800 Subject: [PATCH 3/6] test --- src/java2022spring/Test.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29..e9c39ab 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -2,6 +2,6 @@ package java2022spring; public class Test { public static void main(String[] args) { - System.out.println("Hello world!"); + System.out.println("Hello"); } } -- Gitee From 1e390bffcaf44111ce2de48703703fee05437710 Mon Sep 17 00:00:00 2001 From: 10593 <10593@LAPTOP-02BTTG0N> Date: Wed, 25 May 2022 22:55:04 +0800 Subject: [PATCH 4/6] test 2 --- src/java2022spring/ButtonListener.java | 78 ++++++++++++++++++++++++ src/java2022spring/C_ButtonListener.java | 19 ++++++ src/java2022spring/Calculator.java | 7 +++ src/java2022spring/CalculatorView.java | 66 ++++++++++++++++++++ src/java2022spring/Test.java | 2 +- 5 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 src/java2022spring/ButtonListener.java create mode 100644 src/java2022spring/C_ButtonListener.java create mode 100644 src/java2022spring/Calculator.java create mode 100644 src/java2022spring/CalculatorView.java diff --git a/src/java2022spring/ButtonListener.java b/src/java2022spring/ButtonListener.java new file mode 100644 index 0000000..47cfb08 --- /dev/null +++ b/src/java2022spring/ButtonListener.java @@ -0,0 +1,78 @@ +package java2022spring; + +import java.awt.event.*; +import javax.swing.JTextField; + +public class ButtonListener implements ActionListener { + JTextField input_area; + String number1=null; + String operation_sign = null; + + public void setInputArea(JTextField in) { + input_area = in; + } + + @Override + public void actionPerformed(ActionEvent e) { + String button_name = e.getActionCommand(); //借鉴 + String regex = "[1234567890.]"; + String regex2 = "[\\+\\-\\*\\/]"; + if(button_name.matches(regex)) { + input_area.setText(input_area.getText()+button_name); //借鉴 + } + else if(button_name.matches(regex2)) { + number1=input_area.getText(); + operation_sign = button_name; + input_area.setText(""); + } + + //点=后,计算+-*/法 + if(button_name.equals("=")) { + Double a = Double.valueOf(number1); + Double b = Double.valueOf(input_area.getText()); + Double c = null; + switch(operation_sign) { + case "+": + c=a+b;break; + case "-": + c=a-b;break; + case "*": + c=a*b;break; + case "/": + c=a/b;break; + default:break; + } + input_area.setText(c.toString()); + + //点击%后的计算 + } + else if(button_name.equals("%")) { + Double a = Double.valueOf(input_area.getText()); + a = a/100; + String b = ""; + b = String.format("%.2f",a)+"%"; + input_area.setText(b); + } + + //点击根号后的计算 + else if(button_name.equals("√")) { + Double a = Double.valueOf(input_area.getText()); + a = Math.sqrt(a); + input_area.setText(a.toString()); + } + + //点击+/-号后的计算 + else if(button_name.equals("+/-")) { + Double a = Double.valueOf(input_area.getText()); + a = -a; + input_area.setText(a.toString()); + } + + //点击“后退”的计算 + else if(button_name.equals("后退")) { + String a = input_area.getText(); + input_area.setText(a.substring(0,a.length()-1)); + } + } + +} diff --git a/src/java2022spring/C_ButtonListener.java b/src/java2022spring/C_ButtonListener.java new file mode 100644 index 0000000..ceb5c05 --- /dev/null +++ b/src/java2022spring/C_ButtonListener.java @@ -0,0 +1,19 @@ +package java2022spring; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.JTextField; + +public class C_ButtonListener implements ActionListener { + JTextField input_area; + public void setInputArea(JTextField in) { //将CalculatorView中的input_area传递进来 + input_area = in; + } + + @Override + public void actionPerformed(ActionEvent e) { + input_area.setText(""); + } + +} diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java new file mode 100644 index 0000000..c4fd907 --- /dev/null +++ b/src/java2022spring/Calculator.java @@ -0,0 +1,7 @@ +package java2022spring; + +public class Calculator { + public static void main(String args[]){ + CalculatorView view = new CalculatorView(); + } +} \ No newline at end of file diff --git a/src/java2022spring/CalculatorView.java b/src/java2022spring/CalculatorView.java new file mode 100644 index 0000000..512ca23 --- /dev/null +++ b/src/java2022spring/CalculatorView.java @@ -0,0 +1,66 @@ +package java2022spring; + +import javax.swing.*; +import java.awt.*; + +public class CalculatorView extends JFrame { //设置界面 + //窗体布局的北面的组件 + public JPanel panel_north = new JPanel(); + public JTextField input_area = new JTextField(19); + public JButton c_button = new JButton("C"); + + //窗体布局的中心的组件 + public JPanel panel_center = new JPanel(); + + //构造方法 + public CalculatorView() { + this.init(); + validate(); + } + + //初始化窗体 + public void init() { + this.setTitle("计算器"); //设置窗体 + this.setSize(400, 400); + this.setLayout(new BorderLayout()); + this.setResizable(true); + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); //居中(借鉴) + this.setPanelNorth(); + this.setPanelCenter(); + this.setVisible(true); + } + + //向窗体布局的北面添加组件 + public void setPanelNorth() { + C_ButtonListener c_bl = new C_ButtonListener(); + panel_north.add(input_area); + panel_north.add(c_button); + this.add(panel_north,BorderLayout.NORTH); + this.c_button.setForeground(Color.red); //将清空键设置为红色(借鉴) + this.c_button.addActionListener(c_bl); + c_bl.setInputArea(input_area); + } + + //向窗体布局的中心添加组件 + public void setPanelCenter() { + ButtonListener buttonlistener = new ButtonListener(); //创建一个buttonlistener对象用来监听布局中心的按钮 + String button_text = "123 %456√,789+-0.=*/"; //将按钮的名字连起来变成一个字符串,名字多于一个字符的,先用“ ”和“,”标记,后面修改 + this.panel_center.setLayout(new GridLayout(4,5)); + for(int i=0;i<20;i++) { + String text = button_text.substring(i, i+1); + if(text.equals(" ")) { + text="后退"; //将空格改为后退 + } + if(text.equals(",")) { + text="+/-"; //将,改为+/- + } + JButton center_button = new JButton(); + center_button.setText(text); + panel_center.add(center_button); + center_button.addActionListener(buttonlistener); //center_button注册监听器 + } + this.add(panel_center,BorderLayout.CENTER); + buttonlistener.setInputArea(input_area); //将input_area传递到ButtonListener类里 + } +} \ No newline at end of file diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index e9c39ab..24deb29 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -2,6 +2,6 @@ package java2022spring; public class Test { public static void main(String[] args) { - System.out.println("Hello"); + System.out.println("Hello world!"); } } -- Gitee From 0faa40449bd03074a7fc4b59c2d12c361b68976e Mon Sep 17 00:00:00 2001 From: 10593 <10593@LAPTOP-02BTTG0N> Date: Wed, 8 Jun 2022 20:56:58 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=9C=AC=E6=AC=A1=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=94=B9=E5=8A=A8=E5=BE=88=E5=A4=A7=EF=BC=8C=E4=B8=BB=E8=A6=81?= =?UTF-8?q?=E6=9C=89=E4=BB=A5=E4=B8=8B=E6=96=B9=E9=9D=A2=EF=BC=9A=201?= =?UTF-8?q?=E3=80=81=E6=94=B9=E5=8F=98=E4=BA=86=E8=AE=A1=E7=AE=97=E5=99=A8?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=EF=BC=8C=E6=9B=B4=E7=BE=8E=E8=A7=82=202?= =?UTF-8?q?=E3=80=81=E5=A2=9E=E5=8A=A0=E4=BA=86=E8=AE=A1=E7=AE=97=E5=99=A8?= =?UTF-8?q?=E7=9A=84=E9=83=A8=E5=88=86=E5=8A=9F=E8=83=BD=EF=BC=88=E4=B8=BB?= =?UTF-8?q?=E8=A6=81=E6=98=AF=E8=BF=90=E7=AE=97=E7=AC=A6=E5=8F=B7=E6=96=B9?= =?UTF-8?q?=E9=9D=A2=EF=BC=89=203=E3=80=81=E5=A2=9E=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E8=AE=B8=E5=A4=9A=E5=BC=82=E5=B8=B8=E7=9A=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=204=E3=80=81=E5=87=8F=E5=B0=91=E4=BA=86=E5=8C=97=E9=9D=A2?= =?UTF-8?q?=E7=9A=84C=E6=8C=89=E9=92=AE=EF=BC=8C=E5=B9=B6=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=88=B0=E9=9D=A2=E6=9D=BF=E7=9A=84=E4=B8=AD=E5=BF=83?= =?UTF-8?q?=E5=B8=83=E5=B1=80=E4=B8=AD=E3=80=82=E5=90=8C=E6=97=B6=E6=8A=8A?= =?UTF-8?q?=E5=8E=9F=E6=9C=AC=E5=B1=9E=E4=BA=8EC=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E5=88=A0=E9=99=A4=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/ButtonListener.java | 166 +++++++++++++++++------ src/java2022spring/C_ButtonListener.java | 19 --- src/java2022spring/Calculator.java | 2 +- src/java2022spring/CalculatorView.java | 109 +++++++++++---- 4 files changed, 203 insertions(+), 93 deletions(-) delete mode 100644 src/java2022spring/C_ButtonListener.java diff --git a/src/java2022spring/ButtonListener.java b/src/java2022spring/ButtonListener.java index 47cfb08..64421de 100644 --- a/src/java2022spring/ButtonListener.java +++ b/src/java2022spring/ButtonListener.java @@ -1,78 +1,154 @@ package java2022spring; import java.awt.event.*; + +import javax.swing.JOptionPane; import javax.swing.JTextField; +import java.math.*; public class ButtonListener implements ActionListener { - JTextField input_area; - String number1=null; - String operation_sign = null; + JTextField input_area; //input_area文本框实体用来存放从Calculator传递过来的文本框实体 + String number1=null; //创建一个String类型的number1对象用来记录第一个输入的数字 + String operation_sign = null; //创建一个String类型的operation_sign对象用来记录输入的符号 - public void setInputArea(JTextField in) { + public void setInputArea(JTextField in) { //方法:将Calculator类中的文本框对象传递到ButtonListener类中 input_area = in; } @Override public void actionPerformed(ActionEvent e) { - String button_name = e.getActionCommand(); //借鉴 - String regex = "[1234567890.]"; - String regex2 = "[\\+\\-\\*\\/]"; - if(button_name.matches(regex)) { - input_area.setText(input_area.getText()+button_name); //借鉴 + try { //捕捉过程中出现的异常 + String button_name = e.getActionCommand(); //(借鉴)得到按钮e上的字符串 + String regex = "[1234567890.π]"; //将数字、小数点和π归为一类 + String regex2 = "[\\+\\-\\*\\÷]"; //将+-*÷归为一类 + String regex3 = "log|ln|n\\^√x|x\\^n"; //将log、ln、n^√x、x^n归为一类 + if(button_name.matches(regex)) { //如果按下的按钮是数字、小数点和π中的一个,则将所按下的按钮的文本输入到文本框中 + if(button_name.equals("π")) { //如果按下的按钮是π + button_name = "3.1415926"; //则将按钮的文本改为“3.1415926” + } + input_area.setText(input_area.getText()+button_name); //(借鉴)用加号连接已有文本和所按下的按钮的文本,从而得到新的数字 + //(例子:输入“1”,再输入“2”,则得到“12”) } - else if(button_name.matches(regex2)) { - number1=input_area.getText(); - operation_sign = button_name; - input_area.setText(""); + else if(operation_sign == null){ //如果记录符号的对象operation_sign为空,则执行以下操作 + if(button_name.matches(regex2)||button_name.matches(regex3)) { //如果按下的按钮是运算符号 + number1=input_area.getText(); //记录下第一次输入的数字 + operation_sign = button_name; //记录下所按下的运算符号 + input_area.setText(""); //将文本框清空,以便用户可以输入第二个数字 + } } + else if(!button_name.equals("=")){ //如果记录符号的对象operation_sign不为空,则说明已经点过符号了 + operation_sign = button_name; //将新输入的button_name记录到operation_sign中 + } - //点=后,计算+-*/法 - if(button_name.equals("=")) { - Double a = Double.valueOf(number1); - Double b = Double.valueOf(input_area.getText()); - Double c = null; + //点=后,进行+、-、*、÷、n^√x(开方)、x^n(乘方)、log(取对数)的计算 + if(button_name.equals("=")) { //如果按下的按钮为“=” + Double a = Double.valueOf(number1); //a用来存放第一次输入的数字,并将String类型转换为Double类型 + Double b = Double.valueOf(input_area.getText()); //b用来存放第二次输入的数字,并将String类型转换为Double类型 + Double c = null; //c用来存放计算后的结果 switch(operation_sign) { - case "+": + case "+": //如果之前输入的符号为“+”,则执行加法运算 c=a+b;break; - case "-": + case "-": //如果之前输入的符号为“-”,则执行减法运算 c=a-b;break; - case "*": + case "*": //如果之前输入的符号为“*”,则执行乘法运算 c=a*b;break; - case "/": + case "÷": //如果之前输入的符号为“÷”,则执行除法运算 c=a/b;break; + case "n^√x": //如果之前输入的符号为“n^√x”,则执行开方运算 + c=Math.pow(a,1/b);break; //(借鉴)用pow()方法来开方 + case "x^n": //如果之前输入的符号为“x^n”,则执行乘方运算 + c=Math.pow(a,b);break; + case "log": //如果之前输入的符号为“log”,则执行对数运算 + c=Math.log(b)/Math.log(a);break; //(借鉴)用对数运算的公式进行对数运算 default:break; } - input_area.setText(c.toString()); + //部分异常的处理 + if(c==1/0.0 && operation_sign=="÷") { //若结果正无穷且运算符号为÷,则发出“0不能作为除数”的警告 + input_area.setText("!警告:0不能作为除数"); + } + if(a==0 && operation_sign=="n^√x") { //被开方数为0且运算符号为n^√x,则发出“被开方数不能为0”的警告 + input_area.setText("!警告:被开方数不能为0"); + } + if(b==0 && operation_sign=="n^√x") { //开方数为0且运算符号为n^√x,则发出“开方数不能为0”的警告 + input_area.setText("!警告:开方数不能为0"); + } + if(c==-1/0.0 && operation_sign=="log") { //若结果负无穷且运算符号为log,则发出“真数不能为0”的警告 + input_area.setText("!警告:真数不能为0"); + } + if(c==-0.0 && operation_sign=="log") { //若结果-0.0且运算符号为log,则发出“底数不能为0”的警告 + input_area.setText("!警告:底数不能为0"); + } + else input_area.setText(c.toString()); //若无上述异常情况,则将计算结果c输入到文本框中 + } //点击%后的计算 + else if(button_name.equals("%")) { //如果之前输入的符号为“%”,则执行取百分数运算 + Double a = Double.valueOf(input_area.getText()); //获取文本框中的数据,并存入a + a = a/100; //将a除100 + String b = ""; //创建String b,用来存放计算结果 + b = String.format("%.2f",a)+"%"; //保留两位小数输出“a%” + input_area.setText(b); //向文本框输出计算结果 } - else if(button_name.equals("%")) { - Double a = Double.valueOf(input_area.getText()); - a = a/100; - String b = ""; - b = String.format("%.2f",a)+"%"; - input_area.setText(b); + + //点击+/-号后的计算 + else if(button_name.equals("+/-")) { //如果之前输入的符号为“+/-”,则执行取相反数运算 + Double a = Double.valueOf(input_area.getText()); //获取文本框中的数据,并存入a + a = -a; //将a变成自己的相反数 + input_area.setText(a.toString()); //将a转换成String类型后,向文本框输出计算结果 } - //点击根号后的计算 - else if(button_name.equals("√")) { - Double a = Double.valueOf(input_area.getText()); - a = Math.sqrt(a); - input_area.setText(a.toString()); + //点击“后退”的处理 + else if(button_name.equals("后退")) { //如果之前输入的符号为“后退”,则执行后退一位处理 + String a = input_area.getText(); //获取文本框中的数据,并存入a + input_area.setText(a.substring(0,a.length()-1)); //向文本框输入计算结果,结果为从0的位置开始到倒数第二个位置之间的字符串 } - //点击+/-号后的计算 - else if(button_name.equals("+/-")) { - Double a = Double.valueOf(input_area.getText()); - a = -a; - input_area.setText(a.toString()); + //点击“ln”的计算 + else if(button_name.equals("ln")) { //如果之前输入的符号为“ln”,则执行底数为e的对数运算 + Double a = Double.valueOf(number1); //获取文本框中的数据,并存入a + a = Math.log(a); //通过log()方法,以e为底数对a取对数 + input_area.setText(""+a); //向文本框输入计算结果,用加号连接,直接将a转换成String类型输出 } - //点击“后退”的计算 - else if(button_name.equals("后退")) { - String a = input_area.getText(); - input_area.setText(a.substring(0,a.length()-1)); + //点击“sin”的运算 + else if(button_name.equals("sin")) { + Double a = Double.valueOf(input_area.getText()); //获取文本框中的数据,并存入a + input_area.setText("sin"+a+"°="+new BigDecimal(Math.sin(Math.PI/180*a))); //向文本框输出计算结果 + } + + //点击“cos”的运算 + else if(button_name.equals("cos")) { + Double a = Double.valueOf(input_area.getText()); //获取文本框中的数据,并存入a + input_area.setText("cos"+a+"°="+new BigDecimal(Math.cos(Math.PI/180*a))); //向文本框输出计算结果 + } + + //点击“tan”的运算 + else if(button_name.equals("tan")) { + Double a = Double.valueOf(input_area.getText()); //获取文本框中的数据,并存入a + input_area.setText("tan"+a+"°="+new BigDecimal(Math.tan(Math.PI/180*a))); //向文本框输出计算结果 + } + + //点击“rad”的运算 + else if(button_name.equals("rad")) { + Double a = Double.valueOf(input_area.getText()); //获取文本框中的数据,并存入a + Double b = Math.toRadians(a); //调用toRadians()函数将角度转化为弧度 + input_area.setText(a+"°="+b+"rad"); //向文本框输出计算结果 + } + + //点击“C”后的处理 + else if(button_name.equals("C")) { //若所按下的按钮为C + input_area.setText(""); //则将文本框清空 + number1 = null; //将用来记录第一次输入的数字的对象清空 + operation_sign = null; //将用来记录所输入符号的对象清空 } } - -} + catch(java.lang.NumberFormatException e1) { + JOptionPane.showMessageDialog(null, "出现错误","错误", JOptionPane.ERROR_MESSAGE); //捕获到错误(例如:输入一个数字后按了两次符号)后弹出对话框,位置为屏幕正中间 + //标题为“错误”,消息为“出现错误” + } + catch(java.lang.NullPointerException e2) { + JOptionPane.showMessageDialog(null, "出现错误","错误", JOptionPane.ERROR_MESSAGE); //捕获到错误(例如:只输入一个数字就按=)后弹出对话框,位置为屏幕正中间 + //标题为“错误”,消息为“出现错误” + } + } +} \ No newline at end of file diff --git a/src/java2022spring/C_ButtonListener.java b/src/java2022spring/C_ButtonListener.java deleted file mode 100644 index ceb5c05..0000000 --- a/src/java2022spring/C_ButtonListener.java +++ /dev/null @@ -1,19 +0,0 @@ -package java2022spring; - -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; - -import javax.swing.JTextField; - -public class C_ButtonListener implements ActionListener { - JTextField input_area; - public void setInputArea(JTextField in) { //将CalculatorView中的input_area传递进来 - input_area = in; - } - - @Override - public void actionPerformed(ActionEvent e) { - input_area.setText(""); - } - -} diff --git a/src/java2022spring/Calculator.java b/src/java2022spring/Calculator.java index c4fd907..0705383 100644 --- a/src/java2022spring/Calculator.java +++ b/src/java2022spring/Calculator.java @@ -2,6 +2,6 @@ package java2022spring; public class Calculator { public static void main(String args[]){ - CalculatorView view = new CalculatorView(); + CalculatorView view = new CalculatorView(); //创建一个CalculatorView类的对象 } } \ No newline at end of file diff --git a/src/java2022spring/CalculatorView.java b/src/java2022spring/CalculatorView.java index 512ca23..04bbc5f 100644 --- a/src/java2022spring/CalculatorView.java +++ b/src/java2022spring/CalculatorView.java @@ -5,12 +5,11 @@ import java.awt.*; public class CalculatorView extends JFrame { //设置界面 //窗体布局的北面的组件 - public JPanel panel_north = new JPanel(); - public JTextField input_area = new JTextField(19); - public JButton c_button = new JButton("C"); + public JPanel panel_north = new JPanel(); //创建一个面板 + public JTextField input_area = new JTextField(50); //创建一个文本框 //窗体布局的中心的组件 - public JPanel panel_center = new JPanel(); + public JPanel panel_center = new JPanel(); //创建一个面板 //构造方法 public CalculatorView() { @@ -21,46 +20,100 @@ public class CalculatorView extends JFrame { //设置界面 //初始化窗体 public void init() { this.setTitle("计算器"); //设置窗体 - this.setSize(400, 400); - this.setLayout(new BorderLayout()); - this.setResizable(true); - this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - this.setLocationRelativeTo(null); //居中(借鉴) - this.setPanelNorth(); - this.setPanelCenter(); - this.setVisible(true); + this.setSize(750, 450); //设置窗体大小 + this.setLayout(new BorderLayout()); //将窗体设置为BorderLayout布局 + this.setResizable(true); //将窗体设置为可以改变大小 + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //将窗体设置为按右上角“×”键后退出计算器 + this.setLocationRelativeTo(null); //将窗体居中(借鉴) + this.setPanelNorth(); //调用下面的方法,设置窗体的北面 + this.setPanelCenter(); //调用下面的方法,设置窗体的中心 + this.setVisible(true); //显示GUI组件 } //向窗体布局的北面添加组件 public void setPanelNorth() { - C_ButtonListener c_bl = new C_ButtonListener(); - panel_north.add(input_area); - panel_north.add(c_button); - this.add(panel_north,BorderLayout.NORTH); - this.c_button.setForeground(Color.red); //将清空键设置为红色(借鉴) - this.c_button.addActionListener(c_bl); - c_bl.setInputArea(input_area); + input_area.setFont(new Font("宋体",Font.BOLD, 25)); //设置文本框中字体的格式 + panel_north.add(input_area); //将文本框添加到panel_north面板 + this.add(panel_north,BorderLayout.NORTH); //将panel_north面板添加到CalculatorView的北面 } - //向窗体布局的中心添加组件 + //向窗体布局的中心添加组件,并对中心的组件进行换文本、换颜色等系列操作 public void setPanelCenter() { + Font font1 = new Font("CourierNew",Font.BOLD,50); //创建字体格式1对象 + Font font2 = new Font("CourierNew",Font.BOLD,25); //创建字体格式2对象 + String regex = "[1234567890.]"; //将数字、小数点归为一类,以便接下来将其按钮改变为白色 + String regex2 = "[\\+\\-\\*\\÷]|\\+/\\-|sin|cos|tan|π|rad|log|ln|n\\^√x|x\\^n"; + //将+-*÷、+/-、sin、cos、tan、π、rad、log、|ln、|n^√x、|x^n归为一类, + //以便接下来将其按钮改变为蓝色,文本改变为白色 + String regex3 = "=|后退|C"; //将=、后退、C归为一类,以便接下来将其按钮改变为橙色,文本改变为白色 ButtonListener buttonlistener = new ButtonListener(); //创建一个buttonlistener对象用来监听布局中心的按钮 - String button_text = "123 %456√,789+-0.=*/"; //将按钮的名字连起来变成一个字符串,名字多于一个字符的,先用“ ”和“,”标记,后面修改 - this.panel_center.setLayout(new GridLayout(4,5)); - for(int i=0;i<20;i++) { - String text = button_text.substring(i, i+1); + String button_text = "123+-no456*÷√^789,sct.0= Cπr"; //(借鉴)将按钮的名字连起来变成一个字符串 + //(非借鉴)名字多于一个字符的,先用“ ”和“,”标记,后面修改后面用if语句修改 + //(非借鉴)将ln、log、n^√x、x^n、sin、cos、tan、rad分别用n、o、√、s、c、t、r来代替,后面用if语句修改 + this.panel_center.setLayout(new GridLayout(4,7)); //将中心的面板设置为网格布局,4行7列 + //(借鉴)用循环的方法式创建按钮并输入文本 + for(int i=0;i<28;i++) { + String text = button_text.substring(i, i+1); //将button_text字符串中的字符一个一个地作为每一个按钮的文本 if(text.equals(" ")) { text="后退"; //将空格改为后退 } if(text.equals(",")) { text="+/-"; //将,改为+/- } - JButton center_button = new JButton(); - center_button.setText(text); - panel_center.add(center_button); + if(text.equals("n")) { + text="ln"; //将n改为ln + } + if(text.equals("o")) { + text="log"; //将o改为log + } + if(text.equals("√")) { + text="n^√x"; //将√改为n^√x + } + if(text.equals("^")) { + text="x^n"; //将^改为x^n + } + if(text.equals("s")) { + text="sin"; //将s改为sin + } + if(text.equals("c")) { + text="cos"; //将c改为cos + } + if(text.equals("t")) { + text="tan"; //将t改为tan + } + if(text.equals("r")) { + text="rad"; //将r改为rad + } + JButton center_button = new JButton(); //创建一个按钮 + center_button.setText(text); //设置按钮上的文本 + panel_center.add(center_button); //向中心的面板加入按钮组件 center_button.addActionListener(buttonlistener); //center_button注册监听器 + //设置按钮颜色和按钮文本的字体格式 + if(text.matches(regex)) { //如果按钮文本为数字、小数点 + center_button.setForeground(Color.black); //则将其文本改变为黑色 + center_button.setBackground(Color.white); //按钮改变为白色 + center_button.setFont(font2); //并将文本的字体格式设置为font2 + } + else if(text.matches(regex2)) { //如果按钮文本为+-*÷、+/-、sin、cos、tan、π、rad、log、|ln、|n^√x、|x^n + center_button.setForeground(Color.white); //则将其文本改变为白色 + center_button.setBackground(new Color(153,198,220));//按钮改变为蓝色 + center_button.setFont(font2); ////并将文本的字体格式设置为font2 + } + else if(text.matches(regex3)) { //如果按钮文本为=、后退、C + center_button.setForeground(Color.white); //则将其文本改变为白色 + center_button.setBackground(new Color(246,145,21));//按钮改变为橙色 + if(text.equals("C")) { //如果按钮的文本text为“C” + center_button.setFont(font1); //则将按钮文本字体格式设置为font1 + } + if(text.equals("=")) { //如果按钮的文本text为“=” + center_button.setFont(font1); //则将按钮文本字体格式设置为font1 + } + if(text.equals("后退")) { ////如果按钮的文本text为“后退” + center_button.setFont(font2); //则将按钮文本字体格式设置为font2 + } + } } - this.add(panel_center,BorderLayout.CENTER); + this.add(panel_center,BorderLayout.CENTER); //将面板加入到BorderLayout布局的中心 buttonlistener.setInputArea(input_area); //将input_area传递到ButtonListener类里 } } \ No newline at end of file -- Gitee From de2b2107d3a42b5d3ad309d840aa11f58df44a98 Mon Sep 17 00:00:00 2001 From: 10593 <10593@LAPTOP-02BTTG0N> Date: Fri, 10 Jun 2022 00:10:38 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=9B=B4=E6=AD=A3=E4=BA=86=E9=83=A8?= =?UTF-8?q?=E5=88=86=E6=B3=A8=E9=87=8A=EF=BC=8C=E6=94=B9=E6=AD=A3=E4=BA=86?= =?UTF-8?q?=E2=80=9C=E9=83=A8=E5=88=86=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= =?UTF-8?q?=E2=80=9D=E4=B8=AD=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/ButtonListener.java | 17 +++++++++-------- src/java2022spring/CalculatorView.java | 9 +++++---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/java2022spring/ButtonListener.java b/src/java2022spring/ButtonListener.java index 64421de..1d1d7e6 100644 --- a/src/java2022spring/ButtonListener.java +++ b/src/java2022spring/ButtonListener.java @@ -7,11 +7,11 @@ import javax.swing.JTextField; import java.math.*; public class ButtonListener implements ActionListener { - JTextField input_area; //input_area文本框实体用来存放从Calculator传递过来的文本框实体 + JTextField input_area; //input_area文本框实体用来存放从CalculatorView传递过来的文本框实体 String number1=null; //创建一个String类型的number1对象用来记录第一个输入的数字 String operation_sign = null; //创建一个String类型的operation_sign对象用来记录输入的符号 - public void setInputArea(JTextField in) { //方法:将Calculator类中的文本框对象传递到ButtonListener类中 + public void setInputArea(JTextField in) { //方法:将CalculatorView类中的文本框对象传递到ButtonListener类中 input_area = in; } @@ -43,6 +43,7 @@ public class ButtonListener implements ActionListener { //点=后,进行+、-、*、÷、n^√x(开方)、x^n(乘方)、log(取对数)的计算 if(button_name.equals("=")) { //如果按下的按钮为“=” Double a = Double.valueOf(number1); //a用来存放第一次输入的数字,并将String类型转换为Double类型 + String d = input_area.getText(); Double b = Double.valueOf(input_area.getText()); //b用来存放第二次输入的数字,并将String类型转换为Double类型 Double c = null; //c用来存放计算后的结果 switch(operation_sign) { @@ -63,22 +64,22 @@ public class ButtonListener implements ActionListener { default:break; } //部分异常的处理 - if(c==1/0.0 && operation_sign=="÷") { //若结果正无穷且运算符号为÷,则发出“0不能作为除数”的警告 + if(d.equals("0") && operation_sign.equals("÷")) { //若结果正无穷且运算符号为÷,则发出“0不能作为除数”的警告 input_area.setText("!警告:0不能作为除数"); } - if(a==0 && operation_sign=="n^√x") { //被开方数为0且运算符号为n^√x,则发出“被开方数不能为0”的警告 + else if(number1.equals("0") && operation_sign.equals("n^√x")) { //被开方数为0且运算符号为n^√x,则发出“被开方数不能为0”的警告 input_area.setText("!警告:被开方数不能为0"); } - if(b==0 && operation_sign=="n^√x") { //开方数为0且运算符号为n^√x,则发出“开方数不能为0”的警告 + else if(d.equals("0") && operation_sign.equals("n^√x")) { //开方数为0且运算符号为n^√x,则发出“开方数不能为0”的警告 input_area.setText("!警告:开方数不能为0"); } - if(c==-1/0.0 && operation_sign=="log") { //若结果负无穷且运算符号为log,则发出“真数不能为0”的警告 + else if(d.equals("0") && operation_sign.equals("log")) { //若结果负无穷且运算符号为log,则发出“真数不能为0”的警告 input_area.setText("!警告:真数不能为0"); } - if(c==-0.0 && operation_sign=="log") { //若结果-0.0且运算符号为log,则发出“底数不能为0”的警告 + else if(c==-0.0 && operation_sign=="log") { //若结果-0.0且运算符号为log,则发出“底数不能为0”的警告 input_area.setText("!警告:底数不能为0"); } - else input_area.setText(c.toString()); //若无上述异常情况,则将计算结果c输入到文本框中 + else input_area.setText(c.toString()); //若无上述异常情况,则将计算结果c输入到文本框中 } //点击%后的计算 diff --git a/src/java2022spring/CalculatorView.java b/src/java2022spring/CalculatorView.java index 04bbc5f..a18f113 100644 --- a/src/java2022spring/CalculatorView.java +++ b/src/java2022spring/CalculatorView.java @@ -19,7 +19,7 @@ public class CalculatorView extends JFrame { //设置界面 //初始化窗体 public void init() { - this.setTitle("计算器"); //设置窗体 + this.setTitle("计算器"); //设置窗体标题 this.setSize(750, 450); //设置窗体大小 this.setLayout(new BorderLayout()); //将窗体设置为BorderLayout布局 this.setResizable(true); //将窗体设置为可以改变大小 @@ -43,13 +43,14 @@ public class CalculatorView extends JFrame { //设置界面 Font font2 = new Font("CourierNew",Font.BOLD,25); //创建字体格式2对象 String regex = "[1234567890.]"; //将数字、小数点归为一类,以便接下来将其按钮改变为白色 String regex2 = "[\\+\\-\\*\\÷]|\\+/\\-|sin|cos|tan|π|rad|log|ln|n\\^√x|x\\^n"; - //将+-*÷、+/-、sin、cos、tan、π、rad、log、|ln、|n^√x、|x^n归为一类, + //将+、-、*、÷、+/-、sin、cos、tan、π、rad、log、ln、n^√x、x^n归为一类, //以便接下来将其按钮改变为蓝色,文本改变为白色 String regex3 = "=|后退|C"; //将=、后退、C归为一类,以便接下来将其按钮改变为橙色,文本改变为白色 ButtonListener buttonlistener = new ButtonListener(); //创建一个buttonlistener对象用来监听布局中心的按钮 String button_text = "123+-no456*÷√^789,sct.0= Cπr"; //(借鉴)将按钮的名字连起来变成一个字符串 //(非借鉴)名字多于一个字符的,先用“ ”和“,”标记,后面修改后面用if语句修改 - //(非借鉴)将ln、log、n^√x、x^n、sin、cos、tan、rad分别用n、o、√、s、c、t、r来代替,后面用if语句修改 + //(非借鉴)将ln、log、n^√x、x^n、sin、cos、tan、rad + //(非借鉴)分别用n、o、√、s、c、t、r来代替,后面用if语句修改 this.panel_center.setLayout(new GridLayout(4,7)); //将中心的面板设置为网格布局,4行7列 //(借鉴)用循环的方法式创建按钮并输入文本 for(int i=0;i<28;i++) { @@ -94,7 +95,7 @@ public class CalculatorView extends JFrame { //设置界面 center_button.setBackground(Color.white); //按钮改变为白色 center_button.setFont(font2); //并将文本的字体格式设置为font2 } - else if(text.matches(regex2)) { //如果按钮文本为+-*÷、+/-、sin、cos、tan、π、rad、log、|ln、|n^√x、|x^n + else if(text.matches(regex2)) { //如果按钮文本为+-*÷、+/-、sin、cos、tan、π、rad、log、ln、、n^√x、x^n center_button.setForeground(Color.white); //则将其文本改变为白色 center_button.setBackground(new Color(153,198,220));//按钮改变为蓝色 center_button.setFont(font2); ////并将文本的字体格式设置为font2 -- Gitee