From bd01e273aa8cb6df070dd76a9fd59571548e6e27 Mon Sep 17 00:00:00 2001 From: blackjack <10921033+MUDLI@user.noreply.gitee.com> Date: Wed, 18 May 2022 22:57:42 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E7=AB=8B=E9=A1=B9,=E5=88=9D=E6=AD=A5?= =?UTF-8?q?=E5=BB=BA=E7=AB=8B=E9=A1=B9=E7=9B=AE=E6=96=87=E4=BB=B6=EF=BC=8C?= =?UTF-8?q?=E4=BB=A5=E5=9F=BA=E7=A1=80=E8=AE=A1=E7=AE=97=E5=99=A8UI?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E7=9A=84=E5=AE=8C=E6=88=90=E4=BD=9C=E9=A2=84?= =?UTF-8?q?=E8=AE=A1=E5=88=9D=E6=AD=A5=E5=B7=A5=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++ .idea/inspectionProfiles/Project_Default.xml | 36 +++++ .idea/misc.xml | 6 + .idea/modules.xml | 8 ++ .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 6 + 2022_Spring_0109_calculator.iml | 11 ++ src/AppMain/Main.java | 14 ++ .../SimpleCalculatorUI/Panel/BottomPanel.java | 13 ++ .../SimpleCalculatorUI/Panel/ForePanel.java | 13 ++ src/UI/SimpleCalculatorUI/Panel/KeyPanel.java | 13 ++ .../SimpleCalculatorUI/Panel/OptionPanel.java | 13 ++ .../Panel/ScreenPanel_First.java | 13 ++ .../Panel/ScreenPanel_Second.java | 13 ++ src/UI/SimpleCalculatorUI/Win.java | 13 ++ 15 files changed, 304 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 2022_Spring_0109_calculator.iml create mode 100644 src/AppMain/Main.java create mode 100644 src/UI/SimpleCalculatorUI/Panel/BottomPanel.java create mode 100644 src/UI/SimpleCalculatorUI/Panel/ForePanel.java create mode 100644 src/UI/SimpleCalculatorUI/Panel/KeyPanel.java create mode 100644 src/UI/SimpleCalculatorUI/Panel/OptionPanel.java create mode 100644 src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java create mode 100644 src/UI/SimpleCalculatorUI/Panel/ScreenPanel_Second.java create mode 100644 src/UI/SimpleCalculatorUI/Win.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..9654793 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,36 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /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..f88aa83 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/2022_Spring_0109_calculator.iml b/2022_Spring_0109_calculator.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/2022_Spring_0109_calculator.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/AppMain/Main.java b/src/AppMain/Main.java new file mode 100644 index 0000000..3cac798 --- /dev/null +++ b/src/AppMain/Main.java @@ -0,0 +1,14 @@ +package AppMain; + +/** + * @ClassName:Main + * @Description:应用入口 + * @Author:LMD傻蛋 + * @Date:2022/5/18 22 42 + * @version:v1.0 + */ +public class Main { + public static void main(String[] args) { + + } +} diff --git a/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java b/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java new file mode 100644 index 0000000..d1eec18 --- /dev/null +++ b/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java @@ -0,0 +1,13 @@ +package UI.SimpleCalculatorUI.Panel; + +import javax.swing.*; + +/** + * @ClassName:BottomPanel + * @Description:底层面板&背景面板 + * @Author:LMD傻蛋 + * @Date:2022/5/18 22 43 + * @version:v1.0 + */ +public class BottomPanel extends JPanel { +} diff --git a/src/UI/SimpleCalculatorUI/Panel/ForePanel.java b/src/UI/SimpleCalculatorUI/Panel/ForePanel.java new file mode 100644 index 0000000..51dcc47 --- /dev/null +++ b/src/UI/SimpleCalculatorUI/Panel/ForePanel.java @@ -0,0 +1,13 @@ +package UI.SimpleCalculatorUI.Panel; + +import javax.swing.*; + +/** + * @ClassName:ForePanel + * @Description:前端面板,将键盘面板、显示面板、设置面板组合在一起 + * @Author:LMD傻蛋 + * @Date:2022/5/18 22 44 + * @version:v1.0 + */ +public class ForePanel extends JPanel { +} diff --git a/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java b/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java new file mode 100644 index 0000000..8af2e6f --- /dev/null +++ b/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java @@ -0,0 +1,13 @@ +package UI.SimpleCalculatorUI.Panel; + +import javax.swing.*; + +/** + * @ClassName:KeyPanel + * @Description:键盘面板,按键组合在一起形成 + * @Author:LMD傻蛋 + * @Date:2022/5/18 22 45 + * @version:v1.0 + */ +public class KeyPanel extends JPanel { +} diff --git a/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java b/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java new file mode 100644 index 0000000..117192d --- /dev/null +++ b/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java @@ -0,0 +1,13 @@ +package UI.SimpleCalculatorUI.Panel; + +import javax.swing.*; + +/** + * @ClassName:OptionPanel + * @Description:设置面板,一个小框,放移动条、关闭、最小化按钮 + * @Author:LMD傻蛋 + * @Date:2022/5/18 22 50 + * @version:v1.0 + */ +public class OptionPanel extends JPanel { +} diff --git a/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java new file mode 100644 index 0000000..7962b78 --- /dev/null +++ b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java @@ -0,0 +1,13 @@ +package UI.SimpleCalculatorUI.Panel; + +import javax.swing.*; + +/** + * @ClassName:ScreenPanel_First + * @Description:显示面板,数字在此面板显示 + * @Author:LMD傻蛋 + * @Date:2022/5/18 22 45 + * @version:v1.0 + */ +public class ScreenPanel_First extends JPanel { +} diff --git a/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_Second.java b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_Second.java new file mode 100644 index 0000000..fcee213 --- /dev/null +++ b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_Second.java @@ -0,0 +1,13 @@ +package UI.SimpleCalculatorUI.Panel; + +import javax.swing.*; + +/** + * @ClassName:ScreenPanel_Second + * @Description:显示面板,ScreenPanel_First显示设置窗 + * @Author:LMD傻蛋 + * @Date:2022/5/18 22 47 + * @version:v1.0 + */ +public class ScreenPanel_Second extends JPanel { +} diff --git a/src/UI/SimpleCalculatorUI/Win.java b/src/UI/SimpleCalculatorUI/Win.java new file mode 100644 index 0000000..c331c5e --- /dev/null +++ b/src/UI/SimpleCalculatorUI/Win.java @@ -0,0 +1,13 @@ +package UI.SimpleCalculatorUI; + +import javax.swing.*; + +/** + * @ClassName:Win + * @Description:基础计算器的窗口 + * @Author:LMD傻蛋 + * @Date:2022/5/18 22 40 + * @version:v1.0 + */ +public class Win extends JFrame { +} -- Gitee From b570a11aa421681acbef92f3c635f85274b75d4e Mon Sep 17 00:00:00 2001 From: blackjack <10921033+MUDLI@user.noreply.gitee.com> Date: Thu, 19 May 2022 00:18:35 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E5=BA=95=E5=B1=82=E9=9D=A2=E6=9D=BF=E2=80=94?= =?UTF-8?q?=E2=80=94=E8=83=8C=E6=99=AF=E7=BB=98=E5=88=B6=E5=AE=8C=E6=AF=95?= =?UTF-8?q?=EF=BC=8C=E4=B8=94=E5=8A=A0=E5=85=A5=E5=88=B0=E7=AA=97=E4=BD=93?= =?UTF-8?q?=E4=B8=AD=EF=BC=8C=E5=8E=BB=E6=8E=89=E7=AA=97=E4=BD=93=E6=A0=87?= =?UTF-8?q?=E9=A2=98=E4=B8=94=E9=80=8F=E6=98=8E=E5=8C=96=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E5=90=8E=E7=BB=AD=E7=BB=84=E4=BB=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=88=B0=E5=BA=95=E5=B1=82=E9=9D=A2=E6=9D=BF=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AppMain/Main.java | 4 ++- .../SimpleCalculatorUI/Panel/BottomPanel.java | 35 +++++++++++++++++++ src/UI/SimpleCalculatorUI/Win.java | 32 +++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/AppMain/Main.java b/src/AppMain/Main.java index 3cac798..4550f93 100644 --- a/src/AppMain/Main.java +++ b/src/AppMain/Main.java @@ -1,5 +1,7 @@ package AppMain; +import UI.SimpleCalculatorUI.Win; + /** * @ClassName:Main * @Description:应用入口 @@ -9,6 +11,6 @@ package AppMain; */ public class Main { public static void main(String[] args) { - + new Win(500, 100, 450, 680); } } diff --git a/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java b/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java index d1eec18..b3cd30b 100644 --- a/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java @@ -1,6 +1,7 @@ package UI.SimpleCalculatorUI.Panel; import javax.swing.*; +import java.awt.*; /** * @ClassName:BottomPanel @@ -10,4 +11,38 @@ import javax.swing.*; * @version:v1.0 */ public class BottomPanel extends JPanel { + int BottomPanel_X; + int BottomPanel_Y; + int BottomPanel_Width; + int BottomPanel_Height; + int backGroundArc = 50;//背景圆角弧度 + Color backGroundColor = new Color(40, 51, 69);//背景颜色 + + + public BottomPanel(int x, int y, int width, int height) { + this.BottomPanel_X = x; + this.BottomPanel_Y = y; + this.BottomPanel_Width = width; + this.BottomPanel_Height = height; + init(); + } + + private void init() { + this.setBounds(BottomPanel_X, BottomPanel_Y, + BottomPanel_Width, BottomPanel_Height); + } + + + @Override + protected void paintComponent(Graphics g) { +// setOpaque(false);//组件透明 +// super.paintComponent(g); + g.setColor(backGroundColor);//设置画笔颜色 + g.fillRoundRect(BottomPanel_X, BottomPanel_Y, BottomPanel_Width, + BottomPanel_Height, backGroundArc, backGroundArc);//画出背景 +// ((Graphics2D) g).setRenderingHint( +// RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿 +// ((Graphics2D) g).setStroke( +// new BasicStroke(3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));//边线圆滑 + } } diff --git a/src/UI/SimpleCalculatorUI/Win.java b/src/UI/SimpleCalculatorUI/Win.java index c331c5e..8846389 100644 --- a/src/UI/SimpleCalculatorUI/Win.java +++ b/src/UI/SimpleCalculatorUI/Win.java @@ -1,6 +1,9 @@ package UI.SimpleCalculatorUI; +import UI.SimpleCalculatorUI.Panel.BottomPanel; + import javax.swing.*; +import java.awt.*; /** * @ClassName:Win @@ -10,4 +13,33 @@ import javax.swing.*; * @version:v1.0 */ public class Win extends JFrame { + //窗体坐标与长度、宽度 + int Win_X; + int Win_Y; + int Win_Width; + int Win_Height; + + BottomPanel bottomPanel;//声明基面板 + + + public Win(int win_X, int win_Y, int + win_Width, int win_Height) throws HeadlessException { + this.Win_X = win_X; + this.Win_Y = win_Y; + this.Win_Width = win_Width; + this.Win_Height = win_Height; + init(); + } + + public void init() { + setBounds(Win_X, Win_Y, Win_Width, Win_Height);//设置窗体位置与大小 + bottomPanel = new BottomPanel(0, 0, Win_Width, Win_Height); + add(bottomPanel);//添加底部面板 + setResizable(false);//不可缩放 + setUndecorated(true);//无标题 + setBackground(new Color(255, 255, 255, 0));//背景透明 + setVisible(true);//可视化 + setDefaultCloseOperation(EXIT_ON_CLOSE);//点X关闭 + } } + -- Gitee From 71da742f9e1b4dc5af00265f51ef26aa5298a982 Mon Sep 17 00:00:00 2001 From: blackjack <10921033+MUDLI@user.noreply.gitee.com> Date: Thu, 19 May 2022 00:30:35 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E5=9F=BA=E7=A1=80=E8=AE=A1=E7=AE=97=E5=99=A8?= =?UTF-8?q?UI=E9=83=A8=E5=88=86:=E6=8C=89=E9=94=AE=E9=9D=A2=E6=9D=BF?= =?UTF-8?q?=E7=BB=98=E5=88=B6=E5=AE=8C=E6=88=90=EF=BC=8C=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E4=B9=8B=E9=97=B4=E7=9A=84=E5=B5=8C=E5=A5=97=E5=85=B3=E7=B3=BB?= =?UTF-8?q?=E5=88=9D=E6=AD=A5=E8=AE=BE=E5=AE=9A=E5=AE=8C=E6=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AppMain/Main.java | 2 +- .../SimpleCalculatorUI/Panel/BottomPanel.java | 6 +- .../SimpleCalculatorUI/Panel/ForePanel.java | 34 +++++ src/UI/SimpleCalculatorUI/Panel/KeyPanel.java | 122 ++++++++++++++++++ .../SimpleCalculatorUI/Panel/OptionPanel.java | 20 +++ .../Panel/ScreenPanel_First.java | 16 ++- src/UI/SimpleCalculatorUI/Win.java | 1 + 7 files changed, 198 insertions(+), 3 deletions(-) diff --git a/src/AppMain/Main.java b/src/AppMain/Main.java index 4550f93..cc77862 100644 --- a/src/AppMain/Main.java +++ b/src/AppMain/Main.java @@ -11,6 +11,6 @@ import UI.SimpleCalculatorUI.Win; */ public class Main { public static void main(String[] args) { - new Win(500, 100, 450, 680); + new Win(500, 100, 450, 700); } } diff --git a/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java b/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java index b3cd30b..32b3e24 100644 --- a/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/BottomPanel.java @@ -15,7 +15,8 @@ public class BottomPanel extends JPanel { int BottomPanel_Y; int BottomPanel_Width; int BottomPanel_Height; - int backGroundArc = 50;//背景圆角弧度 + int backGroundArc = 100;//背景圆角弧度 + ForePanel forePanel;//表置面板 Color backGroundColor = new Color(40, 51, 69);//背景颜色 @@ -28,6 +29,9 @@ public class BottomPanel extends JPanel { } private void init() { + forePanel = new ForePanel(0, 0, BottomPanel_Width, BottomPanel_Height); + this.add(forePanel); + this.setLayout(null);//空布局 this.setBounds(BottomPanel_X, BottomPanel_Y, BottomPanel_Width, BottomPanel_Height); } diff --git a/src/UI/SimpleCalculatorUI/Panel/ForePanel.java b/src/UI/SimpleCalculatorUI/Panel/ForePanel.java index 51dcc47..767d240 100644 --- a/src/UI/SimpleCalculatorUI/Panel/ForePanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/ForePanel.java @@ -10,4 +10,38 @@ import javax.swing.*; * @version:v1.0 */ public class ForePanel extends JPanel { + int ForePanel_X; + int ForePanel_Y; + int ForePanel_Width; + int ForePanel_Height; + OptionPanel optionPanel;//设置栏面板 + ScreenPanel_First screenPanel;//显示面板 + KeyPanel keyPanel;//按键面板 + + public ForePanel(int forePanel_X, int forePanel_Y, + int forePanel_Width, int forePanel_Height) { + ForePanel_X = forePanel_X; + ForePanel_Y = forePanel_Y; + ForePanel_Width = forePanel_Width; + ForePanel_Height = forePanel_Height; + init(); + } + + private void init() { + optionPanel = new OptionPanel(0, + 0, this.ForePanel_Width, 60); + screenPanel = new ScreenPanel_First(0, + optionPanel.OptionPanel_Height, this.ForePanel_Width, 120); + keyPanel = new KeyPanel(0, + optionPanel.OptionPanel_Height + screenPanel.h, + ForePanel_Width, 500); + this.add(optionPanel); + this.add(screenPanel); + this.add(keyPanel); + this.setBounds(0, 0, ForePanel_Width, ForePanel_Height); + this.setLayout(null);//设为空布局 + this.setOpaque(false);//设为透明 + } + + } diff --git a/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java b/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java index 8af2e6f..e072acb 100644 --- a/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java @@ -1,6 +1,8 @@ package UI.SimpleCalculatorUI.Panel; import javax.swing.*; +import javax.swing.border.Border; +import java.awt.*; /** * @ClassName:KeyPanel @@ -10,4 +12,124 @@ import javax.swing.*; * @version:v1.0 */ public class KeyPanel extends JPanel { + int KeyPanel_X; + int KeyPanel_Y; + int KeyPanel_Width; + int KeyPanel_Height;//坐标与大小 + int keyArc = 50;//按键圆角的弧长 + int KeyHDistance = 26;//按键横距 + int KeyVDistance = 20;//按键纵距 + int KeySideLen = 80;//按键边长 + Color keyColor = new Color(38, 38, 38);//按键背景 + RoundRectKey[] keys;//按键数组 + + public KeyPanel(int keyPanel_X, int keyPanel_Y, int keyPanel_Width, int keyPanel_Height) { + KeyPanel_X = keyPanel_X; + KeyPanel_Y = keyPanel_Y; + KeyPanel_Width = keyPanel_Width; + KeyPanel_Height = keyPanel_Height; + init(); + } + + private void init() { + setLayout(null);//空布局 + this.setOpaque(false);//透明 + this.setBounds(KeyPanel_X, KeyPanel_Y, KeyPanel_Width, KeyPanel_Height); + setButtons();//创建19个按键 + } + + private void setButtons() { + JPanel panel1 = new JPanel();//按键分放在两个子面板 + JPanel panel2 = new JPanel(); + panel1.setLayout(new GridLayout(4, 4, KeyHDistance, KeyVDistance));//面板1放16个按键,网格布局 + panel2.setLayout(null);//面板2空布局 + panel1.setOpaque(false);//均不可见 + panel2.setOpaque(false); + panel1.setBounds(KeyHDistance, 0, this.KeyPanel_Width - 2 * KeyHDistance, + this.KeyPanel_Height - KeySideLen - KeyVDistance); + panel2.setBounds(KeyHDistance, panel1.getHeight() + KeyVDistance, + KeyPanel_Width - 2 * KeyHDistance, KeySideLen + KeyVDistance); + String[] keyNames = {"C", "Del", "%", "/", + "7", "8", "9", "x", "4", "5", "6", + "-", "1", "2", "3", "+", "0", ".", "="};//各按键名 + keys = new RoundRectKey[19]; + for (int i = 0; i < keys.length; i++) { + keys[i] = new RoundRectKey(); + keys[i].setText(keyNames[i]);//设置按键内容 + keys[i].setPreferredSize(new Dimension(KeySideLen, KeySideLen));//设置按键边长 + if (i < 16) { + panel1.add(keys[i]); + } + } + //最后3个按键分别设置 + keys[16].setBounds(0, 0, KeySideLen, KeySideLen); + keys[17].setBounds(KeySideLen + KeyHDistance, 0, KeySideLen, KeySideLen); + keys[18].setBounds(2 * (KeySideLen + KeyHDistance), 0, 2 * KeySideLen + KeyHDistance, KeySideLen); + setKeyColor(keys);//给特定按键设不同颜色 + panel2.add(keys[16]); + panel2.add(keys[17]); + panel2.add(keys[18]); + this.add(panel1); + this.add(panel2); + } + + private void setKeyColor(RoundRectKey[] keys) {//特别案件颜色方法 + for (int i = 0; i < keys.length; i++) { + if ("+-x/%".contains(keys[i].getText())) { + keys[i].setForeground(new Color(228, 121, 48)); + } + if ("=".equals(keys[i].getText())) { + keys[i].setForeground(new Color(255, 0, 0)); + } + if ("C".equals(keys[i].getText())) { + keys[i].setForeground(Color.CYAN); + } + if ("Del".equals(keys[i].getText())) { + keys[i].setForeground(new Color(255, 255, 102)); + } + } + } + + class RoundRectKey extends JButton {//按键样式 + + class RoundRectBorder implements Border {//按键边界 + + @Override + public Insets getBorderInsets(Component c) { + return new Insets(0, 0, 0, 0); + } + + @Override + public boolean isBorderOpaque() { + return false; + } + + @Override + public void paintBorder(Component c, Graphics g, int x, int y, + int width, int height) { + //使用黑色在组件的外边缘绘制一个圆角矩形 + g.setColor(keyColor); + g.drawRoundRect(0, 0, c.getWidth(), c.getHeight(), keyArc, keyArc); + } + } + + public RoundRectKey() { + setMargin(new Insets(0, 0, 0, 0));//去除文字与按钮的边沿 + setBorder(new RoundRectBorder());//圆角矩形边界 + setContentAreaFilled(false);//取消原先画矩形的设置 + setBorderPainted(false);//隐藏按钮明显边界 + setFocusPainted(false);//去除文字周围的虚线框 + setBackground(keyColor);//设置按键背景色 + setForeground(Color.WHITE);//按键内容颜色 + setFont(new Font("等线(正文)", Font.PLAIN, 30));//设置字体 + } + + protected void paintComponent(Graphics g) {//填充按钮 + + g.setColor(getBackground()); + g.fillRoundRect(0, 0, getSize().width, getSize().height, keyArc, keyArc);//填充圆角矩形边界 + // 绘制按钮 + super.paintComponent(g); + } + } } diff --git a/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java b/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java index 117192d..50f0251 100644 --- a/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java @@ -10,4 +10,24 @@ import javax.swing.*; * @version:v1.0 */ public class OptionPanel extends JPanel { + + int OptionPanel_X; + int OptionPanel_Y; + int OptionPanel_Width; + int OptionPanel_Height; + + public OptionPanel(int optionPanel_X, int optionPanel_Y, + int optionPanel_Width, int optionPanel_Height) { + OptionPanel_X = optionPanel_X; + OptionPanel_Y = optionPanel_Y; + OptionPanel_Width = optionPanel_Width; + OptionPanel_Height = optionPanel_Height; + init(); + } + + private void init() { + setOpaque(false);//透明 + setBounds(OptionPanel_X, OptionPanel_Y, OptionPanel_Width, OptionPanel_Height); + } } + diff --git a/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java index 7962b78..6ce056c 100644 --- a/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java +++ b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java @@ -4,10 +4,24 @@ import javax.swing.*; /** * @ClassName:ScreenPanel_First - * @Description:显示面板,数字在此面板显示 + * @Description:显示面板——计算过程的数字在此面板显示 * @Author:LMD傻蛋 * @Date:2022/5/18 22 45 * @version:v1.0 */ public class ScreenPanel_First extends JPanel { + int x, y, w, h; + + public ScreenPanel_First(int x, int y, int w, int h) { + this.x = x; + this.y = y; + this.w = w; + this.h = h; + init(); + } + + private void init() { + setOpaque(false);//透明 + setBounds(x, y, w, h); + } } diff --git a/src/UI/SimpleCalculatorUI/Win.java b/src/UI/SimpleCalculatorUI/Win.java index 8846389..a6fff63 100644 --- a/src/UI/SimpleCalculatorUI/Win.java +++ b/src/UI/SimpleCalculatorUI/Win.java @@ -38,6 +38,7 @@ public class Win extends JFrame { setResizable(false);//不可缩放 setUndecorated(true);//无标题 setBackground(new Color(255, 255, 255, 0));//背景透明 + setLayout(null); setVisible(true);//可视化 setDefaultCloseOperation(EXIT_ON_CLOSE);//点X关闭 } -- Gitee From 986894ccf67ccf0aa1084cf7bd227b8e99a834c4 Mon Sep 17 00:00:00 2001 From: blackjack <10921033+MUDLI@user.noreply.gitee.com> Date: Thu, 19 May 2022 00:34:17 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E5=9F=BA=E7=A1=80=E8=AE=A1=E7=AE=97=E5=99=A8?= =?UTF-8?q?UI=E9=83=A8=E5=88=86:Option=E9=9D=A2=E6=9D=BF=E7=BB=98=E5=88=B6?= =?UTF-8?q?=E5=AE=8C=E6=AF=95=EF=BC=8C=E5=85=B6=E4=B8=AD=E5=90=AB=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E5=8C=96=E6=8C=89=E9=92=AE=E3=80=81=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E3=80=81=E5=85=B3=E9=97=AD=E6=8C=89=E9=92=AE?= =?UTF-8?q?=EF=BC=88=E4=BB=85=E9=9D=A2=E6=9D=BF=E3=80=81=E6=97=A0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SimpleCalculatorUI/Panel/OptionPanel.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java b/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java index 50f0251..eb6904f 100644 --- a/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java @@ -1,6 +1,8 @@ package UI.SimpleCalculatorUI.Panel; import javax.swing.*; +import javax.swing.border.Border; +import java.awt.*; /** * @ClassName:OptionPanel @@ -15,6 +17,9 @@ public class OptionPanel extends JPanel { int OptionPanel_Y; int OptionPanel_Width; int OptionPanel_Height; + JButton minimize;//最小化按钮 + JButton close;//关闭按钮 + JButton menuBar;//菜单键 public OptionPanel(int optionPanel_X, int optionPanel_Y, int optionPanel_Width, int optionPanel_Height) { @@ -26,8 +31,109 @@ public class OptionPanel extends JPanel { } private void init() { + setLayout(null); setOpaque(false);//透明 setBounds(OptionPanel_X, OptionPanel_Y, OptionPanel_Width, OptionPanel_Height); + setButton_Menu();//设置各按钮 + } + + private void setButton_Menu() { + minimize = new RoundButton(); + close = new RoundButton(); + menuBar = new RoundRectButton(); + add(minimize); + add(menuBar); + add(close); + minimize.setBounds(60, 0, 60, 60); + menuBar.setBounds(125, 0, 200, 60); + close.setBounds(330, 0, 60, 60); + minimize.setText("--"); + close.setText("X"); + minimize.setForeground(Color.WHITE); + close.setForeground(Color.RED); + } + + class RoundButton extends JButton {//圆按键(最小化、关闭) + + public RoundButton() { + setFocusPainted(false);//去除文字周围的虚线框 + setBorder(new RoundBorder());//圆边界 + setContentAreaFilled(false);//取消原先画矩形的设置 + setBorderPainted(false);//隐藏按钮明显边界 + setBackground(new Color(175, 171, 171));//设置按键背景色 + setFont(new Font("等线(正文)", Font.PLAIN, 30));//设置字体 + } + + class RoundBorder implements Border {//(按钮)圆边界类 + + @Override + public void paintBorder(Component c, Graphics g, int x, int y, + int width, int height) { + g.setColor(new Color(175, 171, 171)); + g.drawOval(0, 0, c.getWidth(), c.getHeight()); + } + + @Override + public Insets getBorderInsets(Component c) { + return new Insets(0, 0, 0, 0); + } + + @Override + public boolean isBorderOpaque() { + return false; + } + } + + @Override + protected void paintComponent(Graphics g) {//绘制组件使更改内容显示 + g.setColor(getBackground()); + ((Graphics2D) g).setRenderingHint( + RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿 + g.fillOval(0, 0, getSize().width, getSize().height);//填充边界 + super.paintComponent(g);//绘 + } + } + + + class RoundRectButton extends JButton {//圆角矩形按键(菜单) + + class RoundRectBorder implements Border {//圆角矩形边界 + + @Override + public Insets getBorderInsets(Component c) { + return new Insets(0, 0, 0, 0); + } + + @Override + public boolean isBorderOpaque() { + return false; + } + + @Override + public void paintBorder(Component c, Graphics g, int x, int y, + int width, int height) { + //使用黑色在组件的外边缘绘制一个圆角矩形 + g.setColor(new Color(175, 171, 171)); + g.drawRoundRect(0, 0, c.getWidth(), c.getHeight(), 50, 50); + + } + } + + public RoundRectButton() { + setMargin(new Insets(0, 0, 0, 0));//去除文字与按钮的边沿 + setBorder(new RoundRectBorder());//设置圆角矩形边界 + setContentAreaFilled(false);//取消原先画矩形的设置 + setBorderPainted(false);//隐藏按钮明显边界 + setFocusPainted(false);//去除文字周围的虚线框 + setBackground(new Color(59, 56, 56));//设置按键背景色 + } + + protected void paintComponent(Graphics g) {//绘制 + g.setColor(getBackground()); + g.fillRoundRect(0, 0, getSize().width, getSize().height, + 50, 50);//填充圆角矩形边界 + super.paintComponent(g);//绘 + } } } -- Gitee From d6ef04d59969869d2fb34684f66f5ec7947084c3 Mon Sep 17 00:00:00 2001 From: blackjack <10921033+MUDLI@user.noreply.gitee.com> Date: Thu, 19 May 2022 00:43:29 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E5=9F=BA=E7=A1=80=E8=AE=A1=E7=AE=97=E5=99=A8?= =?UTF-8?q?UI=E9=83=A8=E5=88=86:=E6=98=BE=E7=A4=BA=E9=9D=A2=E6=9D=BF?= =?UTF-8?q?=E7=BB=98=E5=88=B6=E5=AE=8C=E6=88=90=EF=BC=8C=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E9=9D=A2=E6=9D=BF=E7=94=B1=E4=B8=89=E4=B8=AA=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E6=A1=86=E7=BB=84=E6=88=90=EF=BC=8C=E6=96=87=E6=9C=AC=E6=A1=86?= =?UTF-8?q?=E9=80=8F=E6=98=8E=EF=BC=8C=E5=8F=AA=E6=98=BE=E7=A4=BA=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E5=86=85=E5=AE=B9=E3=80=82=E5=BE=AE=E8=B0=83=E9=83=A8?= =?UTF-8?q?=E5=88=86=E7=BB=84=E4=BB=B6=E5=A4=A7=E5=B0=8F=E4=B8=8E=E5=BD=A2?= =?UTF-8?q?=E7=8A=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SimpleCalculatorUI/Panel/ForePanel.java | 4 +- .../SimpleCalculatorUI/Panel/OptionPanel.java | 6 +- .../SimpleCalculatorUI/Panel/ScreenPanel.java | 71 +++++++++++++++++++ .../Panel/ScreenPanel_First.java | 27 ------- 4 files changed, 76 insertions(+), 32 deletions(-) create mode 100644 src/UI/SimpleCalculatorUI/Panel/ScreenPanel.java delete mode 100644 src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java diff --git a/src/UI/SimpleCalculatorUI/Panel/ForePanel.java b/src/UI/SimpleCalculatorUI/Panel/ForePanel.java index 767d240..909f573 100644 --- a/src/UI/SimpleCalculatorUI/Panel/ForePanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/ForePanel.java @@ -15,7 +15,7 @@ public class ForePanel extends JPanel { int ForePanel_Width; int ForePanel_Height; OptionPanel optionPanel;//设置栏面板 - ScreenPanel_First screenPanel;//显示面板 + ScreenPanel screenPanel;//显示面板 KeyPanel keyPanel;//按键面板 public ForePanel(int forePanel_X, int forePanel_Y, @@ -30,7 +30,7 @@ public class ForePanel extends JPanel { private void init() { optionPanel = new OptionPanel(0, 0, this.ForePanel_Width, 60); - screenPanel = new ScreenPanel_First(0, + screenPanel = new ScreenPanel(0, optionPanel.OptionPanel_Height, this.ForePanel_Width, 120); keyPanel = new KeyPanel(0, optionPanel.OptionPanel_Height + screenPanel.h, diff --git a/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java b/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java index eb6904f..6a3619e 100644 --- a/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java @@ -45,7 +45,7 @@ public class OptionPanel extends JPanel { add(menuBar); add(close); minimize.setBounds(60, 0, 60, 60); - menuBar.setBounds(125, 0, 200, 60); + menuBar.setBounds(125, 0, 200, 40); close.setBounds(330, 0, 60, 60); minimize.setText("--"); close.setText("X"); @@ -114,7 +114,7 @@ public class OptionPanel extends JPanel { int width, int height) { //使用黑色在组件的外边缘绘制一个圆角矩形 g.setColor(new Color(175, 171, 171)); - g.drawRoundRect(0, 0, c.getWidth(), c.getHeight(), 50, 50); + g.drawRoundRect(0, 0, c.getWidth(), c.getHeight(), 20, 20); } } @@ -131,7 +131,7 @@ public class OptionPanel extends JPanel { protected void paintComponent(Graphics g) {//绘制 g.setColor(getBackground()); g.fillRoundRect(0, 0, getSize().width, getSize().height, - 50, 50);//填充圆角矩形边界 + 20, 20);//填充圆角矩形边界 super.paintComponent(g);//绘 } } diff --git a/src/UI/SimpleCalculatorUI/Panel/ScreenPanel.java b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel.java new file mode 100644 index 0000000..8fbe8b0 --- /dev/null +++ b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel.java @@ -0,0 +1,71 @@ +package UI.SimpleCalculatorUI.Panel; + +import javax.swing.*; +import java.awt.*; + +/** + * @ClassName:ScreenPanel_First + * @Description:显示面板——计算过程的数字在此面板显示 + * @Author:LMD傻蛋 + * @Date:2022/5/18 22 45 + * @version:v1.0 + */ +public class ScreenPanel extends JPanel { + int x, y, w, h; + JPanel showArea;//显示面板,便于将显示文本框放置 + JTextField showTextHigh;//顶中下三个文本框 + JTextField showTextMid; + JTextField showTextLow; + + public void setShowArea() { + showArea = new JPanel(); + showArea.setOpaque(false);//显示面板透明 + showArea.setLayout(null); + //面板大小,宽度450,左右留白26的宽 + showArea.setBounds(26, 10, 398, 90); + showTextHigh = new JTextField(); + showTextMid = new JTextField(); + showTextLow = new JTextField(); + showTextHigh.setBounds(0, 0, 398, 30); + showTextMid.setBounds(0, 30, 398, 30); + showTextLow.setBounds(0, 60, 398, 30); + showArea.add(showTextHigh); + showArea.add(showTextMid); + showArea.add(showTextLow); + Font showTextFont = new Font("等线(正文)", Font.PLAIN, 35);//显示字体 + showTextHigh.setFont(showTextFont); + showTextMid.setFont(showTextFont); + showTextLow.setFont(showTextFont); + showTextHigh.setOpaque(false);//文本框不可见 + showTextMid.setOpaque(false); + showTextLow.setOpaque(false); + showTextHigh.setForeground(Color.GRAY);//顶部文本框字体灰色 + showTextMid.setForeground(Color.LIGHT_GRAY);//中间轻灰 + showTextLow.setForeground(Color.WHITE);//下部输入数字在此显示,白色 + showTextHigh.setHorizontalAlignment(JTextField.RIGHT);//从右往左输入 + showTextMid.setHorizontalAlignment(JTextField.RIGHT); + showTextLow.setHorizontalAlignment(JTextField.RIGHT); + showTextHigh.setBorder(null);//去掉文本边框 + showTextMid.setBorder(null); + showTextLow.setBorder(null); + showTextHigh.setFocusable(false);//不可聚焦 + showTextMid.setFocusable(false); + showTextLow.setFocusable(false); + } + + public ScreenPanel(int x, int y, int w, int h) { + this.x = x; + this.y = y; + this.w = w; + this.h = h; + init(); + } + + private void init() { + setLayout(null); + setOpaque(false);//本面板透明 + setBounds(x, y, w, h); + setShowArea(); + add(showArea);//显示面板加入到此面板 + } +} diff --git a/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java deleted file mode 100644 index 6ce056c..0000000 --- a/src/UI/SimpleCalculatorUI/Panel/ScreenPanel_First.java +++ /dev/null @@ -1,27 +0,0 @@ -package UI.SimpleCalculatorUI.Panel; - -import javax.swing.*; - -/** - * @ClassName:ScreenPanel_First - * @Description:显示面板——计算过程的数字在此面板显示 - * @Author:LMD傻蛋 - * @Date:2022/5/18 22 45 - * @version:v1.0 - */ -public class ScreenPanel_First extends JPanel { - int x, y, w, h; - - public ScreenPanel_First(int x, int y, int w, int h) { - this.x = x; - this.y = y; - this.w = w; - this.h = h; - init(); - } - - private void init() { - setOpaque(false);//透明 - setBounds(x, y, w, h); - } -} -- Gitee From 8d5c155a0b1090ec236d43b2a5b84f69e60e24cf Mon Sep 17 00:00:00 2001 From: blackjack <10921033+MUDLI@user.noreply.gitee.com> Date: Thu, 19 May 2022 00:55:31 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E5=9F=BA=E7=A1=80=E8=AE=A1=E7=AE=97=E5=99=A8?= =?UTF-8?q?=E6=89=80=E6=9C=89=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0=E5=AE=8C?= =?UTF-8?q?=E6=88=90=EF=BC=8C=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=AE=8C=E6=88=90=EF=BC=8C=E5=8A=A0=E5=87=8F=E4=B9=98?= =?UTF-8?q?=E9=99=A4=E5=8F=96=E4=BD=99=E8=AE=A1=E7=AE=97=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E7=BB=84=E5=90=88=E5=AE=8C=E6=88=90=EF=BC=8C=E5=90=84=E9=9D=A2?= =?UTF-8?q?=E7=89=88=E5=BE=AE=E8=B0=83=EF=BC=8C=E6=B7=BB=E5=8A=A0=E7=9B=91?= =?UTF-8?q?=E5=90=AC=E5=99=A8=E7=AD=89=E3=80=82=E5=8F=AF=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E7=9A=84=E7=AE=80=E5=8D=95=E7=9A=84=E5=8A=A0?= =?UTF-8?q?=E5=87=8F=E4=B9=98=E9=99=A4=E5=8F=96=E4=BD=99=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Function/Function.java | 34 +++ src/SwingEvents/Key_screenListener.java | 236 ++++++++++++++++++ src/UI/SimpleCalculatorUI/Panel/KeyPanel.java | 10 +- .../SimpleCalculatorUI/Panel/OptionPanel.java | 6 +- .../SimpleCalculatorUI/Panel/ScreenPanel.java | 7 +- 5 files changed, 288 insertions(+), 5 deletions(-) create mode 100644 src/Function/Function.java create mode 100644 src/SwingEvents/Key_screenListener.java diff --git a/src/Function/Function.java b/src/Function/Function.java new file mode 100644 index 0000000..06e088f --- /dev/null +++ b/src/Function/Function.java @@ -0,0 +1,34 @@ +package Function; + +/** + * @ClassName:Function + * @Description:加减乘除等各计算方法的集合 + * @Author:LMD傻蛋 + * @Date:2022/5/19 00 44 + * @version:v1.0 + */ +public class Function { + public Function() { + + } + + public double add(double x, double y) {//加 + return x + y; + } + + public double subtract(double x, double y) { + return x - y; + }//减 + + public double multiply(double x, double y) { + return x * y; + }//乘 + + public double divide(double x, double y) { + return x / y; + }//除 + + public double modulo(double x, double y) {//取模 + return x % y; + } +} diff --git a/src/SwingEvents/Key_screenListener.java b/src/SwingEvents/Key_screenListener.java new file mode 100644 index 0000000..8dc2e32 --- /dev/null +++ b/src/SwingEvents/Key_screenListener.java @@ -0,0 +1,236 @@ +package SwingEvents; + +import Function.Function; +import UI.SimpleCalculatorUI.Panel.ScreenPanel; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.math.BigDecimal; + +/** + * @ClassName:Key_screenListener + * @Description:监听键盘与显示屏行为,做出相应反应 + * @Author:LMD傻蛋 + * @Date:2022/5/19 00 50 + * @version:v1.0 + */ +/* +1.点击0-9数字,在showTextLow显示 + 1.1限制显示位数 +2.在数字后跟“+-x/%.”正常显示 + 2.1无数字时输入“+-”作正负号,其他在初始数字“0”后跟 +3.单击“=”showTextLow显示结果,计算式上移一行。 +4.再输入,结果并入中间行,底行显示新输入 +5.点击“C”,清空所有行内容,底行显示0 +6.点击“Del”,逐字删除底行内容,直到删完置0,但不影响其他行 + */ +public class Key_screenListener implements ActionListener { + JTextField showHigh = ScreenPanel.showTextHigh; + JTextField showMid = ScreenPanel.showTextMid; + JTextField showLow = ScreenPanel.showTextLow; + StringBuffer highText;//各自显示文本 + StringBuffer midText; + StringBuffer lowText; + Function function;//计算方法类 + double result = 0;//计算结果 + + public Key_screenListener() { + init(); + } + + + void init() { + setText(); + function = new Function();//计算方法集合 + } + + void setText() { + highText = new StringBuffer(); + midText = new StringBuffer(); + lowText = new StringBuffer(); + } + + @Override + public void actionPerformed(ActionEvent e) { + JButton button = (JButton) e.getSource(); + String buttonText = button.getText();//获得按键内容 + String lowTextLastChar;//底行显示文本最后一个字符 + if (buttonText.matches("[0-9]")) {//按下数字的情况 + if (lowText.length() < 15) {//限制数字长度 + lowText.append(buttonText);//按下数字后底文本记录 + //中显示框等号右边无数字时按下数字键,等号右边将添加计算结果 + if (midText.length() != 0 && midText.toString(). + charAt(midText.length() - 1) == '=') { + midText.append(result); + showMid.setText(midText.toString()); + lowText = new StringBuffer(buttonText); + } + } + showLow.setText(lowText.toString());//显示底屏幕 + } + if (buttonText.matches("[-+x/%]")) {//按下运算符的情况 + if (lowText.toString().length() != 0) {//长度不为0,判断最后字符是不是运算符 + lowTextLastChar = Character.toString( + lowText.toString().charAt(lowText.length() - 1)); + + if (!("-+x/%.".contains(lowTextLastChar))) {//两个数之间只能有一个运算符且小数点后不能接运算符 + String s = lowText.toString(); + int count = 0; + //“+”“-”不是第一个字符则一个式子只能有一个运算符 + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == 'x') { + count++; + } else if (s.charAt(i) == '/') { + count++; + } else if (s.charAt(i) == '%') { + count++; + } else if (i != 0 && s.charAt(i) == '+') { + count++; + } else if (i != 0 && s.charAt(i) == '-') { + count++; + } + } + if (count == 0) {//按下运算符键时底行文本无运算符才可以添加运算符,即一行只有一个计算符 + lowText.append(buttonText); + } + } + } else {//长度为0,按下+-,表示正负号,按下其他如“%”,则0% + if ("+-".contains(buttonText)) { + lowText.append(buttonText); + } else { + lowText.append("0"); + lowText.append(buttonText); + } + } + //按下运算符,中显示行等号右边无数字时添加上上次计算的计算结果,底行新起计算 + if (midText.length() != 0 && midText.toString(). + charAt(midText.length() - 1) == '=') { + midText.append(result); + showMid.setText(midText.toString()); + } + showLow.setText(lowText.toString());//用上次计算结果来进行下次计算,底行不变 + } + if (buttonText.equals(".")) {//按下小数点 + if (lowText.length() == 0) {//按下小数点,底行文本为0时,添加0,变成“0.” + lowText.append("0"); + } + //将底文本进行分割成两个操作数,分别只能有1个小数点 + String s = lowText.toString(); + String[] operand = s.split("[-+x/%]");//lowText用操作符分割成运算数 + if (operand.length == 1) {//只有一个操作数时 + if (!operand[0].contains(".")) { + lowTextLastChar = Character.toString( + lowText.toString().charAt(lowText.length() - 1)); + if (!lowTextLastChar.matches("[-+x/%]")) {//小数点前一位不能是运算符 + lowText.append(buttonText); + } + } + } + if (operand.length == 2) {//有两个操作数时,不需考虑操作数1 + if (!operand[1].contains(".")) { + lowTextLastChar = Character.toString( + lowText.toString().charAt(lowText.length() - 1)); + if (!lowTextLastChar.matches("[-+x/%]")) {//小数点前一位不能是运算符 + lowText.append(buttonText); + } + } + } + showLow.setText(lowText.toString()); + } + if (buttonText.equals("C")) {//按下C初始化显示 + lowText = new StringBuffer(""); + midText = new StringBuffer(""); + highText = new StringBuffer(""); + showLow.setText("0"); + showMid.setText(midText.toString()); + showHigh.setText(highText.toString()); + } + if (buttonText.equals("Del")) {//按下Def键,逐字符删除 + if (lowText.length() > 0) {//有字符时删除 + lowText.deleteCharAt(lowText.length() - 1); + showLow.setText(lowText.toString()); + } + if (lowText.length() == 0) {//无字符时补0 + showLow.setText("0"); + } + } + //按下“=” + //1.无操作符时不改变 + //2.有但只有一个操作符也不改变 + //3. 计算完底行显示结果,计算式移到上一行 + if (buttonText.equals("=")) {//按下“=”的情况 + String s = lowText.toString(); + int count = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == 'x') { + count++; + } else if (s.charAt(i) == '/') { + count++; + } else if (s.charAt(i) == '%') { + count++; + } else if (i != 0 && s.charAt(i) == '+') { + count++; + } else if (i != 0 && s.charAt(i) == '-') { + count++; + } + } + if (count != 0) { + String[] operations = s.split("[-+x/%]"); + if (operations.length != 1) {//操作数大于1个才可 + lowText.append(buttonText); + if (showMid.getText().length() != 0) {//中文本长度不为0,按下“=”则中文本内容移到上文本 + highText = midText; + showHigh.setText(highText.toString()); + } + midText = lowText;//底文本的计算式移到中文本 + showMid.setText(midText.toString()); + calculate();//进行计算 + lowText = new StringBuffer(Double.toString(result));//底显示行仅显示计算结果 + showLow.setText(lowText.toString()); + } + } + } + } + + void calculate() {//计算 + String s = lowText.toString(); + String[] operations = s.split("[-+x/%=]"); + double operation1 = 0;//得到底文本的两个操作数 + double operation2 = 0; + //第一个操作数若是负数,第一个串会是空串,忽略掉 + if (operations[0].length() != 0) {//无空串 + operation1 = Double.parseDouble(operations[0]); + operation2 = Double.parseDouble(operations[1]); + } else {//有空串 + operation1 = Double.parseDouble(operations[1]); + operation2 = Double.parseDouble(operations[2]); + if (s.charAt(0) == '-') {//首字符是‘-’,操作数为负数 + operation1 = -operation1; + } + } + //底行首字符是正负号情况下,分别讨论后面操作符不同时的运算 + //其他的底行中至多只有一个,不需分别讨论 + if (s.charAt(0) == '+' && (s.substring(1).contains("+"))) { + result = function.add(operation1, operation2); + } else if (s.charAt(0) == '+' && (s.substring(1).contains("-"))) { + result = function.subtract(operation1, operation2); + } else if (s.charAt(0) == '-' && (s.substring(1).contains("-"))) { + result = function.subtract(operation1, operation2); + } else if (s.charAt(0) == '-' && (s.substring(1).contains("+"))) { + result = function.add(operation1, operation2); + } else if (s.contains("+")) { + result = function.add(operation1, operation2); + } else if (s.contains("-")) { + result = function.subtract(operation1, operation2); + } else if (s.contains("x")) { + result = function.multiply(operation1, operation2); + } else if (s.contains("/")) { + result = function.divide(operation1, operation2); + } else if (s.contains("%")) { + result = function.modulo(operation1, operation2); + } + BigDecimal bigDecimal = new BigDecimal(result);//限制结果最大长度,四舍五入 + result = bigDecimal.setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue(); + } +} diff --git a/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java b/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java index e072acb..b3e698c 100644 --- a/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/KeyPanel.java @@ -1,5 +1,7 @@ package UI.SimpleCalculatorUI.Panel; +import SwingEvents.Key_screenListener; + import javax.swing.*; import javax.swing.border.Border; import java.awt.*; @@ -22,6 +24,7 @@ public class KeyPanel extends JPanel { int KeySideLen = 80;//按键边长 Color keyColor = new Color(38, 38, 38);//按键背景 RoundRectKey[] keys;//按键数组 + Key_screenListener key_screenListener = new Key_screenListener();//按键监听 public KeyPanel(int keyPanel_X, int keyPanel_Y, int keyPanel_Width, int keyPanel_Height) { KeyPanel_X = keyPanel_X; @@ -122,11 +125,16 @@ public class KeyPanel extends JPanel { setBackground(keyColor);//设置按键背景色 setForeground(Color.WHITE);//按键内容颜色 setFont(new Font("等线(正文)", Font.PLAIN, 30));//设置字体 + addActionListener(key_screenListener);//添加按键监听 } protected void paintComponent(Graphics g) {//填充按钮 + if (getModel().isArmed()) {//按键被按下显绿色 + g.setColor(Color.GREEN); + } else { + g.setColor(getBackground()); + } - g.setColor(getBackground()); g.fillRoundRect(0, 0, getSize().width, getSize().height, keyArc, keyArc);//填充圆角矩形边界 // 绘制按钮 super.paintComponent(g); diff --git a/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java b/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java index 6a3619e..9df9c3c 100644 --- a/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/OptionPanel.java @@ -86,7 +86,11 @@ public class OptionPanel extends JPanel { @Override protected void paintComponent(Graphics g) {//绘制组件使更改内容显示 - g.setColor(getBackground()); + if (getModel().isArmed()) {//按下按键显色否则为背景色 + g.setColor(Color.RED); + } else { + g.setColor(getBackground()); + } ((Graphics2D) g).setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);//抗锯齿 g.fillOval(0, 0, getSize().width, getSize().height);//填充边界 diff --git a/src/UI/SimpleCalculatorUI/Panel/ScreenPanel.java b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel.java index 8fbe8b0..662362c 100644 --- a/src/UI/SimpleCalculatorUI/Panel/ScreenPanel.java +++ b/src/UI/SimpleCalculatorUI/Panel/ScreenPanel.java @@ -13,9 +13,9 @@ import java.awt.*; public class ScreenPanel extends JPanel { int x, y, w, h; JPanel showArea;//显示面板,便于将显示文本框放置 - JTextField showTextHigh;//顶中下三个文本框 - JTextField showTextMid; - JTextField showTextLow; + static public JTextField showTextHigh;//顶中下三个文本框 + static public JTextField showTextMid; + static public JTextField showTextLow; public void setShowArea() { showArea = new JPanel(); @@ -26,6 +26,7 @@ public class ScreenPanel extends JPanel { showTextHigh = new JTextField(); showTextMid = new JTextField(); showTextLow = new JTextField(); + showTextLow.setText("0");//初始时显示0 showTextHigh.setBounds(0, 0, 398, 30); showTextMid.setBounds(0, 30, 398, 30); showTextLow.setBounds(0, 60, 398, 30); -- Gitee From 40adcdd9ac1e20ca2d0509f1b5acea374c753857 Mon Sep 17 00:00:00 2001 From: blackjack <10921033+MUDLI@user.noreply.gitee.com> Date: Fri, 20 May 2022 12:10:51 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=EF=BC=8C=E8=AE=BE=E7=BD=AE=E6=A0=8F=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86=EF=BC=8C=E7=82=B9x?= =?UTF-8?q?=E5=85=B3=E9=97=AD=E7=A8=8B=E5=BA=8F=EF=BC=8C=E7=82=B9--?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E5=8C=96=E7=AA=97=E5=8F=A3=EF=BC=8C=E6=8B=96?= =?UTF-8?q?=E5=8A=A8=E5=88=98=E6=B5=B7=E5=8C=BA=E5=9F=9F=E5=8F=AF=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E7=AA=97=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/inspectionProfiles/Project_Default.xml | 2 +- src/AppMain/Main.java | 11 +++++- src/SwingEvents/OptionActionListener.java | 25 ++++++++++++++ src/SwingEvents/OptionMouseListener.java | 33 ++++++++++++++++++ .../SimpleCalculatorUI/Panel/OptionPanel.java | 34 +++++++++++++++---- src/UI/SimpleCalculatorUI/Win.java | 16 +++++++++ src/java2022spring/Test.java | 7 ---- 7 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 src/SwingEvents/OptionActionListener.java create mode 100644 src/SwingEvents/OptionMouseListener.java delete mode 100644 src/java2022spring/Test.java diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 9654793..f3e0cf1 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -30,7 +30,7 @@