From 4cbb046fb13fa1c2ef28622aa1d6cc92fe493fa6 Mon Sep 17 00:00:00 2001 From: lin--jintao <1013240360@qq.com> Date: Sun, 29 May 2022 16:02:43 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E5=85=88=E5=A4=A7=E8=87=B4=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E8=AE=A1=E7=AE=97=E5=99=A8=E7=9A=84=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E5=90=8C=E6=97=B6=E7=BB=98=E5=88=B6=E5=87=BA=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Button.java | 43 ++++++++++++++++++++++++++++++++ src/java2022spring/CalPanel.java | 36 ++++++++++++++++++++++++++ src/java2022spring/Test.java | 31 ++++++++++++++++++++++- 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/java2022spring/Button.java create mode 100644 src/java2022spring/CalPanel.java diff --git a/src/java2022spring/Button.java b/src/java2022spring/Button.java new file mode 100644 index 0000000..00bca8f --- /dev/null +++ b/src/java2022spring/Button.java @@ -0,0 +1,43 @@ +package java2022spring; + +import java.awt.*; + +/** + * @className: Button + * @description: TODO + * @author: Lin + * @create: 2022-05-29 15:47 + **/ + + +//实现计算器按钮功能的类 + +public class Button { + + + //按钮的坐标,按钮的高度与宽度 + int x; + int y; + int height; + int width; + + public Button(int x, int y, int width, int height) { + this.x = x; + this.y = y; + + this.width = width; + this.height = height; + + } + + + //创建按钮的同时绘制矩形 + public void paint(Graphics g){ + + g.setColor(Color.ORANGE); + g.fillRect(x, y, width, height); + + g.setColor(Color.BLUE); + g.drawRect(x,y, width, height); + } +} diff --git a/src/java2022spring/CalPanel.java b/src/java2022spring/CalPanel.java new file mode 100644 index 0000000..32bde54 --- /dev/null +++ b/src/java2022spring/CalPanel.java @@ -0,0 +1,36 @@ +package java2022spring; + +import javax.swing.*; +import java.awt.*; + +/** + * @className: CalPanel + * @description: TODO + * @author: Lin + * @create: 2022-05-29 15:32 + **/ + + +//通过继承Jpanel来绘制我们计算器的图形 + +public class CalPanel extends JPanel { + + + + + //在我们的panel里面创建出来一个新按钮 + Button button; + + public CalPanel() { + + + button = new Button(100, 100, 80, 60);; + } + + //重写我们JPanel中的paint方法 + + public void paint(Graphics g){ + + button.paint(g); + } +} diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29..e27e89e 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,7 +1,36 @@ package java2022spring; +import javax.swing.*; + public class Test { public static void main(String[] args) { - System.out.println("Hello world!"); + + + //此处我们通过声明一个Test类的对象,这样的话能使我们后面的代码更为便捷 + Test cal = new Test(); + cal.go(); + + } + + //程序的主要功能 + public void go(){ + + JFrame frame = new JFrame("calculator 20220524"); + //设置我们的关闭按钮,防止我们点完窗口右上角的关闭按钮之后程序仍然在运行 + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //设置窗口的大小 + frame.setSize(500, 600); + + + //将我们绘制的图形填充到我们窗口当中 + CalPanel calpanel = new CalPanel(); + + frame.getContentPane().add(calpanel); + + + frame.setVisible(true); + } + + } -- Gitee From aa16175c2ca3baef459539f5bc422c9aff2d94b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E9=87=91=E6=B6=9B?= <1013240360@qq.com> Date: Sun, 29 May 2022 08:04:26 +0000 Subject: [PATCH 2/7] 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 6e240f8fd94e16eaf0fba126f9b5376871b3aa70 Mon Sep 17 00:00:00 2001 From: lin--jintao <1013240360@qq.com> Date: Mon, 30 May 2022 23:54:49 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E6=8C=89=E9=92=AE?= =?UTF-8?q?=E7=9A=84=E5=9F=BA=E6=9C=AC=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Button.java | 38 ++++++++++++++++++++-- src/java2022spring/CalPanel.java | 56 ++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/src/java2022spring/Button.java b/src/java2022spring/Button.java index 00bca8f..f7c6aae 100644 --- a/src/java2022spring/Button.java +++ b/src/java2022spring/Button.java @@ -1,6 +1,7 @@ package java2022spring; import java.awt.*; +import java.awt.event.MouseEvent; /** * @className: Button @@ -20,7 +21,7 @@ public class Button { int y; int height; int width; - + int state; // state标记鼠标是否在按钮上面,如果是1表示在,如果是0表示不在 public Button(int x, int y, int width, int height) { this.x = x; this.y = y; @@ -34,10 +35,43 @@ public class Button { //创建按钮的同时绘制矩形 public void paint(Graphics g){ - g.setColor(Color.ORANGE); + if (state == 0){ + g.setColor(Color.ORANGE); + } else { + g.setColor(Color.MAGENTA); + } + + g.fillRect(x, y, width, height); g.setColor(Color.BLUE); g.drawRect(x,y, width, height); + + + //在矩形当中填写数字 + g.setColor(Color.BLACK); + Font f = new Font("Arial", Font.PLAIN, 30); + + g.setFont(f); + + FontMetrics fm = g.getFontMetrics(); + int w = fm.stringWidth("8"); + int h = fm.getHeight(); + + g.drawString("8", x + (width - w) / 2, y + (height - h)/ 2); + } + + + public void mouseMoved(MouseEvent e) { + + + //此处我们编写一个算法,检测鼠标是否在按钮当中 + if ( e.getX() < x || e.getX() > x + width || e.getY() < y || e.getY() > y + height){ + + state = 0; + } else { + + state = 1; + } } } diff --git a/src/java2022spring/CalPanel.java b/src/java2022spring/CalPanel.java index 32bde54..5e543bf 100644 --- a/src/java2022spring/CalPanel.java +++ b/src/java2022spring/CalPanel.java @@ -2,6 +2,9 @@ package java2022spring; import javax.swing.*; import java.awt.*; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; /** * @className: CalPanel @@ -13,7 +16,7 @@ import java.awt.*; //通过继承Jpanel来绘制我们计算器的图形 -public class CalPanel extends JPanel { +public class CalPanel extends JPanel implements MouseMotionListener, MouseListener { @@ -24,7 +27,12 @@ public class CalPanel extends JPanel { public CalPanel() { - button = new Button(100, 100, 80, 60);; + button = new Button(100, 100, 80, 60); + + + //这两句语句为了处理鼠标事件, //this指的是CalPanel这个类 + this.addMouseListener(this); + this.addMouseMotionListener(this); } //重写我们JPanel中的paint方法 @@ -33,4 +41,48 @@ public class CalPanel extends JPanel { button.paint(g); } + + @Override + public void mouseDragged(MouseEvent e) { + + } + + /** + * 当鼠标移到按钮上面,就让按钮变幻颜色 + * @param e + */ + @Override + public void mouseMoved(MouseEvent e) { + + //在这里处理鼠标按钮 + button.mouseMoved(e); + + repaint(); + + } + + @Override + public void mouseClicked(MouseEvent e) { + + } + + @Override + public void mousePressed(MouseEvent e) { + + } + + @Override + public void mouseReleased(MouseEvent e) { + + } + + @Override + public void mouseEntered(MouseEvent e) { + + } + + @Override + public void mouseExited(MouseEvent e) { + + } } -- Gitee From b782851a95489cc89caf79eda4bdb88bd1c66f44 Mon Sep 17 00:00:00 2001 From: lin--jintao <1013240360@qq.com> Date: Tue, 31 May 2022 23:06:02 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E6=88=90=E5=8A=9F=E5=B0=86=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E6=9C=BA=E7=9A=84=E7=95=8C=E9=9D=A2=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E5=87=BA=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Button.java | 23 ++++++++++++++--- src/java2022spring/CalPanel.java | 42 +++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/src/java2022spring/Button.java b/src/java2022spring/Button.java index f7c6aae..2486d8b 100644 --- a/src/java2022spring/Button.java +++ b/src/java2022spring/Button.java @@ -21,20 +21,25 @@ public class Button { int y; int height; int width; + + String name; + int state; // state标记鼠标是否在按钮上面,如果是1表示在,如果是0表示不在 - public Button(int x, int y, int width, int height) { + public Button(int x, int y, int width, int height, String name) { this.x = x; this.y = y; this.width = width; this.height = height; + this.name = name; } //创建按钮的同时绘制矩形 public void paint(Graphics g){ + //鼠标移到按钮上面则变色 if (state == 0){ g.setColor(Color.ORANGE); } else { @@ -55,10 +60,10 @@ public class Button { g.setFont(f); FontMetrics fm = g.getFontMetrics(); - int w = fm.stringWidth("8"); + int w = fm.stringWidth(name); int h = fm.getHeight(); - g.drawString("8", x + (width - w) / 2, y + (height - h)/ 2); + g.drawString(name, x + (width - w) / 2, y + (height - h)/ 2 + h); } @@ -74,4 +79,16 @@ public class Button { state = 1; } } + + + public void mousePressed(MouseEvent e) { + + + if ( state == 1){ + + System.out.println(name); + } + + } + } diff --git a/src/java2022spring/CalPanel.java b/src/java2022spring/CalPanel.java index 5e543bf..fff02d4 100644 --- a/src/java2022spring/CalPanel.java +++ b/src/java2022spring/CalPanel.java @@ -19,16 +19,24 @@ import java.awt.event.MouseMotionListener; public class CalPanel extends JPanel implements MouseMotionListener, MouseListener { - - + Button[][] buttons = new Button[4][4]; + String[][] names = { + {"7", "8", "9", "+"}, + {"4", "5", "6", "-"}, + {"1", "2", "3", "X"}, + {".", "0", "=", "/"} + }; //在我们的panel里面创建出来一个新按钮 - Button button; public CalPanel() { + for (int i = 0; i < 4; i++){ - button = new Button(100, 100, 80, 60); + for (int j = 0; j < 4; j++){ + buttons[i][j] = new Button(50 + j * 100, 100 + i * 80, 80, 60, names[i][j]); + } + } //这两句语句为了处理鼠标事件, //this指的是CalPanel这个类 this.addMouseListener(this); @@ -39,7 +47,14 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen public void paint(Graphics g){ - button.paint(g); + // + for (int i = 0; i < 4; i++){ + + for (int j = 0; j < 4; j++){ + + buttons[i][j].paint(g); + } + } } @Override @@ -55,8 +70,13 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen public void mouseMoved(MouseEvent e) { //在这里处理鼠标按钮 - button.mouseMoved(e); + for (int i = 0; i < 4; i++){ + + for (int j = 0; j < 4; j++){ + buttons[i][j].mouseMoved(e); + } + } repaint(); } @@ -69,6 +89,16 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen @Override public void mousePressed(MouseEvent e) { + // + for (int i = 0; i < 4; i++){ + + for (int j = 0; j < 4; j++){ + + buttons[i][j].mousePressed(e); + } + } + + } @Override -- Gitee From c83cc16c1932e7dde8ce8d999de7850b8d139cc5 Mon Sep 17 00:00:00 2001 From: lin--jintao <1013240360@qq.com> Date: Wed, 1 Jun 2022 23:18:55 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E5=99=A8=E7=9A=84=E5=8A=A0=E5=87=8F=E4=B9=98=E9=99=A4=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Button.java | 12 +++- src/java2022spring/CalPanel.java | 102 +++++++++++++++++++++++++++++-- src/java2022spring/Test.java | 2 +- 3 files changed, 108 insertions(+), 8 deletions(-) diff --git a/src/java2022spring/Button.java b/src/java2022spring/Button.java index 2486d8b..169891f 100644 --- a/src/java2022spring/Button.java +++ b/src/java2022spring/Button.java @@ -63,7 +63,7 @@ public class Button { int w = fm.stringWidth(name); int h = fm.getHeight(); - g.drawString(name, x + (width - w) / 2, y + (height - h)/ 2 + h); + g.drawString(name, x + (width - w) / 2, y + (height - h)/ 2 + h * 4 / 5); } @@ -81,14 +81,20 @@ public class Button { } - public void mousePressed(MouseEvent e) { + public boolean mousePressed(MouseEvent e) { + //这里使用了boolean做为返回值,让我们知道哪个键被我们的鼠标按了 + + boolean result = false; + if ( state == 1){ - System.out.println(name); + result = true; } + + return result; } } diff --git a/src/java2022spring/CalPanel.java b/src/java2022spring/CalPanel.java index fff02d4..271512e 100644 --- a/src/java2022spring/CalPanel.java +++ b/src/java2022spring/CalPanel.java @@ -23,9 +23,19 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen String[][] names = { {"7", "8", "9", "+"}, {"4", "5", "6", "-"}, - {"1", "2", "3", "X"}, + {"1", "2", "3", "*"}, {".", "0", "=", "/"} }; + + //点击的哪一个运算符 + String operator = ""; + + //定义两个字符串来保存点击的数字是什么 + String strA = ""; + String strB = ""; + + String strCommand = ""; + //在我们的panel里面创建出来一个新按钮 public CalPanel() { @@ -34,7 +44,7 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen for (int j = 0; j < 4; j++){ - buttons[i][j] = new Button(50 + j * 100, 100 + i * 80, 80, 60, names[i][j]); + buttons[i][j] = new Button(50 + j * 90, 130 + i * 70, 80, 60, names[i][j]); } } @@ -47,7 +57,10 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen public void paint(Graphics g){ - // + //此处对计算机计算框进行清空,防止数字重叠 + + g.clearRect(0, 0, this.getWidth(), this.getHeight()); + for (int i = 0; i < 4; i++){ for (int j = 0; j < 4; j++){ @@ -55,6 +68,25 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen buttons[i][j].paint(g); } } + + + + //计算器上边的框 + g.setColor(Color.BLACK); + g.drawRect(50, 50, 350, 70); + + + //将数字显示在计算器上 + g.setColor(Color.BLACK); + Font f = new Font("Arial", Font.PLAIN, 40); + + g.setFont(f); + + FontMetrics fm = g.getFontMetrics(); + int w = fm.stringWidth(this.strCommand); + int h = fm.getHeight(); + + g.drawString(this.strCommand, 50 + (350 - w), 50 + (80 - h)/ 2 + h * 4 / 5); } @Override @@ -94,7 +126,69 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen for (int j = 0; j < 4; j++){ - buttons[i][j].mousePressed(e); + boolean re = buttons[i][j].mousePressed(e); + + if (re == true){ + + //用户当前按的数字是什么 + String name = buttons[i][j].name; + + //判断输入的是第一个数字,还是第二个数字,还是我们的运算符 + + + + if (!this.strA.equals("") && + name.equals("+") || name.equals("-") || name.equals("*") || name.equals("/")){ + + + this.operator = name; + }else if (name.equals("=") && !this.operator.equals("") && this.strB.equals("") == false){ + + int a = Integer.parseInt(this.strA); + int b = Integer.parseInt(this.strB); + + int c = 0; + + if (this.operator.equals("+")){ + + c = a + b; + } else if (this.operator.equals("-")){ + + c = a - b; + + } else if (this.operator.equals("*")) { + + + c = a * b; + + } else if (this.operator.equals("/")){ + + + c = a / b; + } + + this.strCommand = c + ""; + + //再次清空 + this.strA = ""; + this.strB = ""; + this.operator = ""; + + } else if(this.operator.equals("")){ + + this.strA += name; + + this.strCommand = new String(this.strA); + + } else { + + this.strB = name; + + this.strCommand = new String(this.strB); + } + + } + } } diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index e27e89e..8805beb 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -20,7 +20,7 @@ public class Test { //设置我们的关闭按钮,防止我们点完窗口右上角的关闭按钮之后程序仍然在运行 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口的大小 - frame.setSize(500, 600); + frame.setSize(470, 500); //将我们绘制的图形填充到我们窗口当中 -- Gitee From f4a292e3484376747f6bc561add268007699c6b4 Mon Sep 17 00:00:00 2001 From: lin--jintao <1013240360@qq.com> Date: Thu, 2 Jun 2022 23:35:11 +0800 Subject: [PATCH 6/7] =?UTF-8?q?20220602=E6=89=80=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/CalPanel.java | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/java2022spring/CalPanel.java b/src/java2022spring/CalPanel.java index 271512e..68f78ec 100644 --- a/src/java2022spring/CalPanel.java +++ b/src/java2022spring/CalPanel.java @@ -82,6 +82,13 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen g.setFont(f); + + if (this.strCommand.length() > 15){ + this.strCommand = this.strCommand.substring(0, 15); + } + + + FontMetrics fm = g.getFontMetrics(); int w = fm.stringWidth(this.strCommand); int h = fm.getHeight(); @@ -144,10 +151,10 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen this.operator = name; }else if (name.equals("=") && !this.operator.equals("") && this.strB.equals("") == false){ - int a = Integer.parseInt(this.strA); - int b = Integer.parseInt(this.strB); + double a = Double.parseDouble(this.strA); + double b = Double.parseDouble(this.strB); - int c = 0; + double c = 0; if (this.operator.equals("+")){ @@ -176,14 +183,15 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen } else if(this.operator.equals("")){ - this.strA += name; + + this.strA +=name; + this.strCommand = new String(this.strA); } else { - this.strB = name; - + this.strB +=name; this.strCommand = new String(this.strB); } -- Gitee From cff0bb35065adf245fcdf8a90259d57dc3af60fd Mon Sep 17 00:00:00 2001 From: lin--jintao <1013240360@qq.com> Date: Sat, 4 Jun 2022 23:41:56 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E5=AE=8C=E5=85=A8=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E6=9C=BA=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/CalPanel.java | 37 ++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/java2022spring/CalPanel.java b/src/java2022spring/CalPanel.java index 68f78ec..ad713e0 100644 --- a/src/java2022spring/CalPanel.java +++ b/src/java2022spring/CalPanel.java @@ -184,14 +184,47 @@ public class CalPanel extends JPanel implements MouseMotionListener, MouseListen } else if(this.operator.equals("")){ - this.strA +=name; + if (name.equals(".")){ + if (this.strA.equals("")){ + + this.strA = "0."; + + + } else if (this.strA.contains(".") == false){ + + this.strA += name; + + } + + } else { + + this.strA += name; + + } this.strCommand = new String(this.strA); } else { + if (name.equals(".")){ + - this.strB +=name; + if (this.strB.equals("")){ + + this.strB = "0."; + + + } else if (this.strB.contains(".") == false){ + + this.strB += name; + + } + + } else { + + this.strB += name; + + } this.strCommand = new String(this.strB); } -- Gitee