From 68b4a64bd53938ba0fd19d0dc90fd157a428eba2 Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Tue, 31 May 2022 20:37:09 +0800 Subject: [PATCH 01/11] =?UTF-8?q?=E6=A0=87=E5=87=86=E5=9E=8B=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E5=99=A8(=E6=9C=89=E5=8F=82=E8=80=83)=20=E5=BE=85?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=A7=91=E5=AD=A6=E8=AE=A1=E7=AE=97=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Test.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29..b848e89 100644 --- a/src/java2022spring/Test.java +++ b/src/java2022spring/Test.java @@ -1,7 +1,7 @@ package java2022spring; - +import java.awt.*; public class Test { - public static void main(String[] args) { - System.out.println("Hello world!"); - } + public static void main(String args[]) { + new Caculator(); + } } -- Gitee From 31e427114fc7b7b3f63314950810553d8a493c45 Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Tue, 31 May 2022 20:43:24 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E4=BA=86UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Caculator.java | 243 ++++++++++++++++++++++++++++++ 1 file changed, 243 insertions(+) create mode 100644 src/java2022spring/Caculator.java diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java new file mode 100644 index 0000000..4d26fad --- /dev/null +++ b/src/java2022spring/Caculator.java @@ -0,0 +1,243 @@ +package java2022spring; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.*; +import javax.swing.border.LineBorder; + +public class Caculator extends JFrame { + JMenuBar jMenuBar; + JPanel panel;//控制面板 + JMenu jm1,jm2,jm3; + JMenuItem it1,it2,it3; + JTextArea Inputkuang; + JButton jbs[][] = new JButton[6][5]; + String Mem="0";//存储区的值 + Caculator(){ + init(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + try { + UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); + } catch (Exception e) { + e.printStackTrace(); + }//UI风格(有参考) + setVisible(true);//窗口可见 + setLocationRelativeTo(null); //窗口居中(引用) + validate(); + } + void init(){ + //设置窗口标题 + setTitle("计算器"); + //窗口大小 + setSize(450,550); + //布局样式 + setLayout(new BorderLayout()); + //菜单栏 + JMenuBar jMenuBar = new JMenuBar(); + jm1 = new JMenu("查看(V)"); + jm1.setFont(new Font("宋体", Font.BOLD, 14)); + jm2 = new JMenu("编辑(E)"); + jm2.setFont(new Font("宋体", Font.BOLD, 14)); + jm3 = new JMenu("帮助(H)"); + jm3.setFont(new Font("宋体", Font.BOLD, 14)); + jMenuBar.add(jm1); + jMenuBar.add(jm2); + jMenuBar.add(jm3); + it1 = new JMenuItem("标准"); + it2 = new JMenuItem("科学"); + it3 = new JMenuItem("作者:zxy"); + jm1.add(it1); + jm1.add(it2); + jm3.add(it3); + this.setJMenuBar(jMenuBar); + //输出框 + LineBorder border = new LineBorder(Color.LIGHT_GRAY, 4, true);//文本区域边框设置 + Inputkuang = new JTextArea(2, 7); //计算区域 + Inputkuang.setFont(new Font("Calibre", Font.PLAIN, 40));//设置字体、样式、大小 + + Inputkuang.setBorder(border); + //按钮 + String [][] str = { + {"MC", "MR", "MS", "M+", "M-"}, + {"←", "CE", "C", "±", "√"}, + {"7", "8", "9", "÷", "%"}, + {"4", "5", "6", "×", "1/x"}, + {"1", "2", "3", "-", "^2"}, + {"00","0", ".", "+", "="} + };//有参考 + panel = new JPanel(); + panel.setLayout(new GridLayout(6, 5)); + for (int i = 0; i < 6; i++) { //插入各个功能、数字按钮 + for (int j = 0; j < 5; j++) { + jbs[i][j] = new JButton(str[i][j]); + jbs[i][j].setFont(new Font("黑体", Font.BOLD, 30)); //设置按钮字体、样式、大小 + panel.add(jbs[i][j]); + } + } + for(int i = 0,j = 0;j<5;j++){ //将MC,MR,MS,M+,M-按钮的背景色设为亮灰色 + jbs[i][j].setBackground(Color.LIGHT_GRAY); + } + jbs[5][4].setBackground(new Color(240,216,159));//将=按钮的背景色设为淡黄色 + add(Inputkuang, BorderLayout.NORTH); + add(panel); + + Event(); + + } + public void Event() { + for (int i = 2; i < 6; i++) { + for (int j = 0; j < 5; j++) { + //除了”=“和”MC“等功能键,全部设置自己的监视器(参考) + if(i != 5 || j != 4) + jbs[i][j].addActionListener(new TextListener(jbs[i][j], Inputkuang)); + } + } + //清楚存储区数值 + jbs[0][0].addActionListener((e -> Mem = "0")); + //显示存储区数值字符串 + jbs[0][1].addActionListener(e -> Inputkuang.setText(Mem)); + //存储结果 + jbs[0][2].addActionListener(e -> { + Mem = Inputkuang.getText(); + Mem = Mem.substring(Mem.indexOf('\n')+1); //去掉第一行字符串,只留下结果 + }); + jbs[0][3].addActionListener(e -> { + double x1, x2; + x1 = Double.parseDouble(Mem); + x2 = Double.parseDouble(Inputkuang.getText()); + Mem = "" + (x1 + x2); + });//M+ + jbs[0][4].addActionListener(e -> { + double x1, x2; + x1 = Double.parseDouble(Mem); + x2 = Double.parseDouble(Inputkuang.getText()); + Mem = "" + (x2 - x1); + });//M- + jbs[1][0].addActionListener(new BackListener(Inputkuang));//← + jbs[1][1].addActionListener(new BackListener(Inputkuang));//CE + jbs[1][2].addActionListener(new ClearListener(Inputkuang));//C + jbs[1][3].addActionListener(new TextListener(jbs[1][3], Inputkuang));//± + jbs[1][4].addActionListener(new TextListener(jbs[1][4], Inputkuang));//√ + jbs[5][4].addActionListener(new ResultListener(Inputkuang));//= + } + +} +class ResultListener implements ActionListener { //结果监视器 + JTextArea textArea; + String str; //用来保存文本区域的字符串; + public ResultListener(JTextArea textArea1) { //将文本区传入 + textArea = textArea1; + } + public void actionPerformed(ActionEvent e) { + textArea.setText(textArea.getText() + "="); //获取文本内容 + str = textArea.getText(); + + if (str.contains("+")) { //如果字符串中包含“+”,做加法运算 + String str1 = str.substring(0, str.indexOf('+')); //获取加数 + String str2 = str.substring(str.indexOf('+') + 1, str.indexOf('=')); //获取被加数 + double x1 = Double.parseDouble(str1); //转化为double类型 + double x2 = Double.parseDouble(str2); + textArea.setText(str + "\n" + (x1 + x2)); + } + else if (str.contains("×")) { //乘法运算 + String str1 = str.substring(0, str.indexOf('×')); + String str2 = str.substring(str.indexOf('×') + 1, str.indexOf('=')); + double x1 = Double.parseDouble(str1); + double x2 = Double.parseDouble(str2); + textArea.setText(str + "\n" + x1 * x2); + } + else if(str.contains("1/x")) { //倒数运算 + String str1 = str.substring(0, str.indexOf("1/x")); + double x1 = Double.parseDouble(str1); + if(x1 == 0) textArea.setText(str + "\n" + "不能求倒数"); + else textArea.setText(str + "\n" + 1/x1); + } + else if (str.contains("÷")) { //除法运算 + String str1 = str.substring(0, str.indexOf('÷')); + String str2 = str.substring(str.indexOf('÷') + 1, str.indexOf('=')); + double x1 = Double.parseDouble(str1); + double x2 = Double.parseDouble(str2); + if (x2 == 0) textArea.setText(str + "\n" + "不能除以0"); + else textArea.setText(str + "\n" + x1 / x2); + } + else if (str.contains("^2")) { //平方运算 + String str1 = str.substring(0, str.indexOf('^')); + double x = Double.parseDouble(str1); + textArea.setText(str + "\n" + x * x); + } + else if (str.contains("√")) { //开方运算 + String str1 = str.substring(str.indexOf('√') + 1, str.indexOf('=')); + double x = Double.parseDouble(str1); + textArea.setText(str + "\n" + Math.sqrt(x)); + } + else if (str.contains("±")) { //加减运算 + String str1 = str.substring(0, str.indexOf('±')); + String str2 = str.substring(str.indexOf('±') + 1, str.indexOf('=')); + double x1 = Double.parseDouble(str1); + double x2 = Double.parseDouble(str2); + if (x2 == 0) textArea.setText(str + "\n" + x1); + else textArea.setText(str + "\n" + (x1 + x2) + " & " + (x1 - x2)); + } + else if (str.contains("%")) { //百分号运算 + String str1 = str.substring(0, str.indexOf('%')); + double x1 = Double.parseDouble(str1); + textArea.setText(str + "\n" + x1/100); + + } + else if (str.contains("-")) { //减法运算 + if(str.charAt(0) == '-') { //如果第一个字符是“-”,形如-2-3形式,就转化成为-(2+3)形式 + String str_x = str; + str = str.substring(1); + String str1 = str.substring(0, str.indexOf('-')); + String str2 = str.substring(str.indexOf('-') + 1, str.indexOf('=')); + double x1 = Double.parseDouble(str1); + double x2 = Double.parseDouble(str2); + textArea.setText(str_x + "\n" + (-x1-x2)); + } + else { + String str1 = str.substring(0, str.indexOf('-')); + String str2 = str.substring(str.indexOf('-') + 1, str.indexOf('=')); + double x1 = Double.parseDouble(str1); + double x2 = Double.parseDouble(str2); + textArea.setText(str + "\n" + (x1-x2)); + } + } + } +} +class ClearListener implements ActionListener { //清除监视器 + JTextArea textArea; + public ClearListener(JTextArea textArea1) { //将按钮和文本区传入 + textArea = textArea1; + } + public void actionPerformed(ActionEvent e) { //设置文本区为空,即清空 + textArea.setText(""); + } +} + +class BackListener implements ActionListener { + JTextArea textArea; + String str; + public BackListener(JTextArea textArea1) { //将文本区传入 + textArea = textArea1; + } + public void actionPerformed(ActionEvent e) { //设置文本区 + str = textArea.getText(); + if(str.length() == 0) textArea.setText(""); //如果文本区内容为空,输出为空 + else { //否则让当前文本区删掉最后一个符号/数字 + str = str.substring(0, str.length()-1); + textArea.setText(str); + } + } +} +class TextListener implements ActionListener { //文本监听器 + JButton button; + JTextArea textArea; + public TextListener(JButton button1, JTextArea textArea1) { //将按钮和文本区传入 + textArea = textArea1; + button = button1; + } + public void actionPerformed(ActionEvent e) { //设置文本区 + textArea.setText(textArea.getText() + button.getText()); + } +} \ No newline at end of file -- Gitee From 25a948f2b419974348de49f3b0d3bd2fe1edfda6 Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Sat, 4 Jun 2022 02:27:57 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=BA=86=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E5=90=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Caculator.java | 137 +++++++++++++++--------------- 1 file changed, 67 insertions(+), 70 deletions(-) diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java index 4d26fad..9657462 100644 --- a/src/java2022spring/Caculator.java +++ b/src/java2022spring/Caculator.java @@ -11,9 +11,9 @@ public class Caculator extends JFrame { JPanel panel;//控制面板 JMenu jm1,jm2,jm3; JMenuItem it1,it2,it3; - JTextArea Inputkuang; - JButton jbs[][] = new JButton[6][5]; - String Mem="0";//存储区的值 + JTextArea Wb;//文本区 + JButton jbs[][] = new JButton[6][5];//有参考 + String Mem="0";//存储区的值,运用于MC等功能键 Caculator(){ init(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -23,7 +23,7 @@ public class Caculator extends JFrame { e.printStackTrace(); }//UI风格(有参考) setVisible(true);//窗口可见 - setLocationRelativeTo(null); //窗口居中(引用) + setLocationRelativeTo(null); //窗口居中 validate(); } void init(){ @@ -53,10 +53,10 @@ public class Caculator extends JFrame { this.setJMenuBar(jMenuBar); //输出框 LineBorder border = new LineBorder(Color.LIGHT_GRAY, 4, true);//文本区域边框设置 - Inputkuang = new JTextArea(2, 7); //计算区域 - Inputkuang.setFont(new Font("Calibre", Font.PLAIN, 40));//设置字体、样式、大小 + Wb = new JTextArea(2, 7); //计算区域 + Wb.setFont(new Font("Calibre", Font.PLAIN, 40));//设置字体、样式、大小 - Inputkuang.setBorder(border); + Wb.setBorder(border); //按钮 String [][] str = { {"MC", "MR", "MS", "M+", "M-"}, @@ -67,7 +67,7 @@ public class Caculator extends JFrame { {"00","0", ".", "+", "="} };//有参考 panel = new JPanel(); - panel.setLayout(new GridLayout(6, 5)); + panel.setLayout(new GridLayout(6, 5));//设置容器为网格布局 for (int i = 0; i < 6; i++) { //插入各个功能、数字按钮 for (int j = 0; j < 5; j++) { jbs[i][j] = new JButton(str[i][j]); @@ -79,113 +79,110 @@ public class Caculator extends JFrame { jbs[i][j].setBackground(Color.LIGHT_GRAY); } jbs[5][4].setBackground(new Color(240,216,159));//将=按钮的背景色设为淡黄色 - add(Inputkuang, BorderLayout.NORTH); - add(panel); + add(Wb, BorderLayout.NORTH);//将输出框置于北端 + add(panel);//将控制面板添加进窗口中 Event(); } - public void Event() { + public void Event() { //设置监听器 for (int i = 2; i < 6; i++) { for (int j = 0; j < 5; j++) { - //除了”=“和”MC“等功能键,全部设置自己的监视器(参考) if(i != 5 || j != 4) - jbs[i][j].addActionListener(new TextListener(jbs[i][j], Inputkuang)); + //给数字与运算符号添加监听器 + jbs[i][j].addActionListener(new TextListener(jbs[i][j], Wb)); } } - //清楚存储区数值 - jbs[0][0].addActionListener((e -> Mem = "0")); - //显示存储区数值字符串 - jbs[0][1].addActionListener(e -> Inputkuang.setText(Mem)); - //存储结果 - jbs[0][2].addActionListener(e -> { - Mem = Inputkuang.getText(); - Mem = Mem.substring(Mem.indexOf('\n')+1); //去掉第一行字符串,只留下结果 + jbs[0][0].addActionListener((e -> Mem = "0")); //MC + jbs[0][1].addActionListener(e -> Wb.setText(Mem)); //MR + jbs[0][2].addActionListener(e -> { //MS + Mem = Wb.getText(); + Mem = Mem.substring(Mem.indexOf('\n')+1); //去掉第一行字符串,只留下结果(参考) }); - jbs[0][3].addActionListener(e -> { + jbs[0][3].addActionListener(e -> { //M+ double x1, x2; x1 = Double.parseDouble(Mem); - x2 = Double.parseDouble(Inputkuang.getText()); + x2 = Double.parseDouble(Wb.getText()); Mem = "" + (x1 + x2); - });//M+ - jbs[0][4].addActionListener(e -> { + }); + jbs[0][4].addActionListener(e -> { //M- double x1, x2; x1 = Double.parseDouble(Mem); - x2 = Double.parseDouble(Inputkuang.getText()); + x2 = Double.parseDouble(Wb.getText()); Mem = "" + (x2 - x1); - });//M- - jbs[1][0].addActionListener(new BackListener(Inputkuang));//← - jbs[1][1].addActionListener(new BackListener(Inputkuang));//CE - jbs[1][2].addActionListener(new ClearListener(Inputkuang));//C - jbs[1][3].addActionListener(new TextListener(jbs[1][3], Inputkuang));//± - jbs[1][4].addActionListener(new TextListener(jbs[1][4], Inputkuang));//√ - jbs[5][4].addActionListener(new ResultListener(Inputkuang));//= + }); + jbs[1][0].addActionListener(new BackListener(Wb));//← + jbs[1][1].addActionListener(new BackListener(Wb));//CE + jbs[1][2].addActionListener(new ClearListener(Wb));//C + jbs[1][3].addActionListener(new TextListener(jbs[1][3], Wb));//± + jbs[1][4].addActionListener(new TextListener(jbs[1][4], Wb));//√ + jbs[5][4].addActionListener(new ResultListener(Wb));//= } } -class ResultListener implements ActionListener { //结果监视器 - JTextArea textArea; - String str; //用来保存文本区域的字符串; - public ResultListener(JTextArea textArea1) { //将文本区传入 - textArea = textArea1; +class ResultListener implements ActionListener { //结果监听器 + JTextArea Wb; + String str; //保存文本区的字符串 + public ResultListener(JTextArea Wb1) { //将文本区传入 + Wb = Wb1; } public void actionPerformed(ActionEvent e) { - textArea.setText(textArea.getText() + "="); //获取文本内容 - str = textArea.getText(); + Wb.setText(Wb.getText() + "="); //获取文本内容 + str = Wb.getText(); if (str.contains("+")) { //如果字符串中包含“+”,做加法运算 String str1 = str.substring(0, str.indexOf('+')); //获取加数 String str2 = str.substring(str.indexOf('+') + 1, str.indexOf('=')); //获取被加数 double x1 = Double.parseDouble(str1); //转化为double类型 double x2 = Double.parseDouble(str2); - textArea.setText(str + "\n" + (x1 + x2)); + Wb.setText(str + "\n" + (x1 + x2)); //输出 } else if (str.contains("×")) { //乘法运算 String str1 = str.substring(0, str.indexOf('×')); String str2 = str.substring(str.indexOf('×') + 1, str.indexOf('=')); double x1 = Double.parseDouble(str1); double x2 = Double.parseDouble(str2); - textArea.setText(str + "\n" + x1 * x2); + Wb.setText(str + "\n" + x1 * x2); } else if(str.contains("1/x")) { //倒数运算 String str1 = str.substring(0, str.indexOf("1/x")); double x1 = Double.parseDouble(str1); - if(x1 == 0) textArea.setText(str + "\n" + "不能求倒数"); - else textArea.setText(str + "\n" + 1/x1); + if(x1 == 0) Wb.setText(str + "\n" + "不能求倒数"); //错误处理 + else Wb.setText(str + "\n" + 1/x1); } else if (str.contains("÷")) { //除法运算 String str1 = str.substring(0, str.indexOf('÷')); String str2 = str.substring(str.indexOf('÷') + 1, str.indexOf('=')); double x1 = Double.parseDouble(str1); double x2 = Double.parseDouble(str2); - if (x2 == 0) textArea.setText(str + "\n" + "不能除以0"); - else textArea.setText(str + "\n" + x1 / x2); + if (x2 == 0) Wb.setText(str + "\n" + "不能除以0"); //错误处理 + else Wb.setText(str + "\n" + x1 / x2); } else if (str.contains("^2")) { //平方运算 String str1 = str.substring(0, str.indexOf('^')); double x = Double.parseDouble(str1); - textArea.setText(str + "\n" + x * x); + Wb.setText(str + "\n" + x * x); } else if (str.contains("√")) { //开方运算 String str1 = str.substring(str.indexOf('√') + 1, str.indexOf('=')); double x = Double.parseDouble(str1); - textArea.setText(str + "\n" + Math.sqrt(x)); + Wb.setText(str + "\n" + Math.sqrt(x)); } else if (str.contains("±")) { //加减运算 String str1 = str.substring(0, str.indexOf('±')); String str2 = str.substring(str.indexOf('±') + 1, str.indexOf('=')); double x1 = Double.parseDouble(str1); double x2 = Double.parseDouble(str2); - if (x2 == 0) textArea.setText(str + "\n" + x1); - else textArea.setText(str + "\n" + (x1 + x2) + " & " + (x1 - x2)); + if (x2 == 0) Wb.setText(str + "\n" + x1); + else Wb.setText(str + "\n" + (x1 + x2) + " & " + (x1 - x2)); } else if (str.contains("%")) { //百分号运算 String str1 = str.substring(0, str.indexOf('%')); double x1 = Double.parseDouble(str1); - textArea.setText(str + "\n" + x1/100); + Wb.setText(str + "\n" + x1/100); } - else if (str.contains("-")) { //减法运算 + else if (str.contains("-")) { //减法运算(参考) if(str.charAt(0) == '-') { //如果第一个字符是“-”,形如-2-3形式,就转化成为-(2+3)形式 String str_x = str; str = str.substring(1); @@ -193,51 +190,51 @@ class ResultListener implements ActionListener { // String str2 = str.substring(str.indexOf('-') + 1, str.indexOf('=')); double x1 = Double.parseDouble(str1); double x2 = Double.parseDouble(str2); - textArea.setText(str_x + "\n" + (-x1-x2)); + Wb.setText(str_x + "\n" + (-x1-x2)); } else { String str1 = str.substring(0, str.indexOf('-')); String str2 = str.substring(str.indexOf('-') + 1, str.indexOf('=')); double x1 = Double.parseDouble(str1); double x2 = Double.parseDouble(str2); - textArea.setText(str + "\n" + (x1-x2)); + Wb.setText(str + "\n" + (x1-x2)); } } } } -class ClearListener implements ActionListener { //清除监视器 - JTextArea textArea; +class ClearListener implements ActionListener { //清除监听器,用于C + JTextArea Wb; public ClearListener(JTextArea textArea1) { //将按钮和文本区传入 - textArea = textArea1; + Wb = textArea1; } - public void actionPerformed(ActionEvent e) { //设置文本区为空,即清空 - textArea.setText(""); + public void actionPerformed(ActionEvent e) { //设置文本区内容为空,即清空 + Wb.setText(""); } } -class BackListener implements ActionListener { - JTextArea textArea; +class BackListener implements ActionListener { //用于←,CE + JTextArea Wb; String str; - public BackListener(JTextArea textArea1) { //将文本区传入 - textArea = textArea1; + public BackListener(JTextArea Wb1) { //将文本区传入 + Wb = Wb1; } public void actionPerformed(ActionEvent e) { //设置文本区 - str = textArea.getText(); - if(str.length() == 0) textArea.setText(""); //如果文本区内容为空,输出为空 + str = Wb.getText(); + if(str.length() == 0) Wb.setText(""); //如果文本区内容为空,输出为空 else { //否则让当前文本区删掉最后一个符号/数字 str = str.substring(0, str.length()-1); - textArea.setText(str); + Wb.setText(str); } } } class TextListener implements ActionListener { //文本监听器 JButton button; - JTextArea textArea; - public TextListener(JButton button1, JTextArea textArea1) { //将按钮和文本区传入 - textArea = textArea1; + JTextArea Wb; + public TextListener(JButton button1, JTextArea Wb1) { //将按钮和文本区传入 + Wb = Wb1; button = button1; } public void actionPerformed(ActionEvent e) { //设置文本区 - textArea.setText(textArea.getText() + button.getText()); + Wb.setText(Wb.getText() + button.getText()); } } \ No newline at end of file -- Gitee From de16f84d8ee4823029236d1e763c97eecbe1f7dd Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Sun, 5 Jun 2022 02:04:38 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E7=A7=91?= =?UTF-8?q?=E5=AD=A6=E8=AE=A1=E7=AE=97=E5=99=A8=E7=9A=84=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=88=E5=BE=85=E5=AE=8C=E5=96=84=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Caculator.java | 116 +++++++++++++++++++----------- 1 file changed, 76 insertions(+), 40 deletions(-) diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java index 9657462..91b4786 100644 --- a/src/java2022spring/Caculator.java +++ b/src/java2022spring/Caculator.java @@ -1,5 +1,6 @@ package java2022spring; + import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -8,31 +9,18 @@ import javax.swing.border.LineBorder; public class Caculator extends JFrame { JMenuBar jMenuBar; - JPanel panel;//控制面板 + JPanel panel1,panel2;//控制面板 JMenu jm1,jm2,jm3; JMenuItem it1,it2,it3; JTextArea Wb;//文本区 - JButton jbs[][] = new JButton[6][5];//有参考 + JButton jbs1[][] = new JButton[6][5];//有参考 + JButton jbs2[][] = new JButton[8][5]; String Mem="0";//存储区的值,运用于MC等功能键 Caculator(){ - init(); + init1(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - try { - UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); - } catch (Exception e) { - e.printStackTrace(); - }//UI风格(有参考) - setVisible(true);//窗口可见 - setLocationRelativeTo(null); //窗口居中 - validate(); - } - void init(){ //设置窗口标题 setTitle("计算器"); - //窗口大小 - setSize(450,550); - //布局样式 - setLayout(new BorderLayout()); //菜单栏 JMenuBar jMenuBar = new JMenuBar(); jm1 = new JMenu("查看(V)"); @@ -51,6 +39,35 @@ public class Caculator extends JFrame { jm1.add(it2); jm3.add(it3); this.setJMenuBar(jMenuBar); + try { + UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); + } catch (Exception e) { + e.printStackTrace(); + }//UI风格(有参考) + it1.addActionListener(new ActionListener() { //标准型 + public void actionPerformed(ActionEvent e) { + panel1.setVisible(true); + panel2.setVisible(false); + init1(); + } + }); + it2.addActionListener(new ActionListener() { //科学型 + public void actionPerformed(ActionEvent e) { + panel2.setVisible(true); + panel1.setVisible(false); + init2(); + } + }); + setVisible(true);//窗口可见 + setLocationRelativeTo(null); //窗口居中 + validate(); + } + void init1(){ //标准型 + //窗口大小 + setSize(450,550); + //布局样式 + setLayout(new BorderLayout()); + //输出框 LineBorder border = new LineBorder(Color.LIGHT_GRAY, 4, true);//文本区域边框设置 Wb = new JTextArea(2, 7); //计算区域 @@ -58,7 +75,7 @@ public class Caculator extends JFrame { Wb.setBorder(border); //按钮 - String [][] str = { + String [][] str1 = { {"MC", "MR", "MS", "M+", "M-"}, {"←", "CE", "C", "±", "√"}, {"7", "8", "9", "÷", "%"}, @@ -66,57 +83,76 @@ public class Caculator extends JFrame { {"1", "2", "3", "-", "^2"}, {"00","0", ".", "+", "="} };//有参考 - panel = new JPanel(); - panel.setLayout(new GridLayout(6, 5));//设置容器为网格布局 + panel1 = new JPanel(); + panel1.setLayout(new GridLayout(6, 5));//设置容器为网格布局 for (int i = 0; i < 6; i++) { //插入各个功能、数字按钮 for (int j = 0; j < 5; j++) { - jbs[i][j] = new JButton(str[i][j]); - jbs[i][j].setFont(new Font("黑体", Font.BOLD, 30)); //设置按钮字体、样式、大小 - panel.add(jbs[i][j]); + jbs1[i][j] = new JButton(str1[i][j]); + jbs1[i][j].setFont(new Font("黑体", Font.BOLD, 30)); //设置按钮字体、样式、大小 + panel1.add(jbs1[i][j]); } } for(int i = 0,j = 0;j<5;j++){ //将MC,MR,MS,M+,M-按钮的背景色设为亮灰色 - jbs[i][j].setBackground(Color.LIGHT_GRAY); + jbs1[i][j].setBackground(Color.LIGHT_GRAY); } - jbs[5][4].setBackground(new Color(240,216,159));//将=按钮的背景色设为淡黄色 + jbs1[5][4].setBackground(new Color(240,216,159));//将=按钮的背景色设为淡黄色 add(Wb, BorderLayout.NORTH);//将输出框置于北端 - add(panel);//将控制面板添加进窗口中 + add(panel1);//将控制面板1添加进窗口中 + + Event1(); - Event(); + } + + void init2() { //科学型 + setSize(500,650); + setLayout(new BorderLayout()); + LineBorder border = new LineBorder(Color.LIGHT_GRAY, 4, true);//文本区域边框设置 + Wb = new JTextArea(2, 7); //计算区域 + Wb.setFont(new Font("Calibre", Font.PLAIN, 40));//设置字体、样式、大小 + Wb.setBorder(border); + String [][] str2 = { + {"MC", "MR", "MS", "M+", "M-"}, + {"←", "CE", "C", "±", "√"}, + {"7", "8", "9", "÷", "%"}, + {"4", "5", "6", "×", "1/x"}, + {"1", "2", "3", "-", "^2"}, + {"00","0", ".", "+", "="} + };//待修改 + } - public void Event() { //设置监听器 + public void Event1() { //设置监听器 for (int i = 2; i < 6; i++) { for (int j = 0; j < 5; j++) { if(i != 5 || j != 4) //给数字与运算符号添加监听器 - jbs[i][j].addActionListener(new TextListener(jbs[i][j], Wb)); + jbs1[i][j].addActionListener(new TextListener(jbs1[i][j], Wb)); } } - jbs[0][0].addActionListener((e -> Mem = "0")); //MC - jbs[0][1].addActionListener(e -> Wb.setText(Mem)); //MR - jbs[0][2].addActionListener(e -> { //MS + jbs1[0][0].addActionListener((e -> Mem = "0")); //MC + jbs1[0][1].addActionListener(e -> Wb.setText(Mem)); //MR + jbs1[0][2].addActionListener(e -> { //MS Mem = Wb.getText(); Mem = Mem.substring(Mem.indexOf('\n')+1); //去掉第一行字符串,只留下结果(参考) }); - jbs[0][3].addActionListener(e -> { //M+ + jbs1[0][3].addActionListener(e -> { //M+ double x1, x2; x1 = Double.parseDouble(Mem); x2 = Double.parseDouble(Wb.getText()); Mem = "" + (x1 + x2); }); - jbs[0][4].addActionListener(e -> { //M- + jbs1[0][4].addActionListener(e -> { //M- double x1, x2; x1 = Double.parseDouble(Mem); x2 = Double.parseDouble(Wb.getText()); Mem = "" + (x2 - x1); }); - jbs[1][0].addActionListener(new BackListener(Wb));//← - jbs[1][1].addActionListener(new BackListener(Wb));//CE - jbs[1][2].addActionListener(new ClearListener(Wb));//C - jbs[1][3].addActionListener(new TextListener(jbs[1][3], Wb));//± - jbs[1][4].addActionListener(new TextListener(jbs[1][4], Wb));//√ - jbs[5][4].addActionListener(new ResultListener(Wb));//= + jbs1[1][0].addActionListener(new BackListener(Wb));//← + jbs1[1][1].addActionListener(new BackListener(Wb));//CE + jbs1[1][2].addActionListener(new ClearListener(Wb));//C + jbs1[1][3].addActionListener(new TextListener(jbs1[1][3], Wb));//± + jbs1[1][4].addActionListener(new TextListener(jbs1[1][4], Wb));//√ + jbs1[5][4].addActionListener(new ResultListener(Wb));//= } } -- Gitee From 645579a22d7dc9ec180f9c186853014328e5ff98 Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Sun, 5 Jun 2022 23:28:01 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E7=A7=91?= =?UTF-8?q?=E5=AD=A6=E8=AE=A1=E7=AE=97=E5=99=A8=E7=9A=84=E9=9D=A2=E6=9D=BF?= =?UTF-8?q?=EF=BC=88=E7=AD=89=E5=BE=85=E5=AE=8C=E5=96=84=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=89=EF=BC=8C=E6=AD=A3=E5=9C=A8=E6=80=9D=E8=80=83=E5=A6=82?= =?UTF-8?q?=E4=BD=95=E5=88=87=E6=8D=A2=E9=9D=A2=E6=9D=BF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Caculator.java | 78 +++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 14 deletions(-) diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java index 91b4786..71e8130 100644 --- a/src/java2022spring/Caculator.java +++ b/src/java2022spring/Caculator.java @@ -17,7 +17,7 @@ public class Caculator extends JFrame { JButton jbs2[][] = new JButton[8][5]; String Mem="0";//存储区的值,运用于MC等功能键 Caculator(){ - init1(); + init2(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口标题 setTitle("计算器"); @@ -44,25 +44,24 @@ public class Caculator extends JFrame { } catch (Exception e) { e.printStackTrace(); }//UI风格(有参考) - it1.addActionListener(new ActionListener() { //标准型 + /*it1.addActionListener(new ActionListener() { //标准型 public void actionPerformed(ActionEvent e) { - panel1.setVisible(true); - panel2.setVisible(false); - init1(); + remove(panel1); + panel1 = FirstPanel.firstView(); + init1(); } }); it2.addActionListener(new ActionListener() { //科学型 public void actionPerformed(ActionEvent e) { - panel2.setVisible(true); - panel1.setVisible(false); - init2(); + remove(panel2); + panel2 = SecondPanel.secondView(); } - }); + });*/ setVisible(true);//窗口可见 setLocationRelativeTo(null); //窗口居中 validate(); } - void init1(){ //标准型 + void init1(){ //标准型 //窗口大小 setSize(450,550); //布局样式 @@ -104,7 +103,7 @@ public class Caculator extends JFrame { } void init2() { //科学型 - setSize(500,650); + setSize(520,650); setLayout(new BorderLayout()); LineBorder border = new LineBorder(Color.LIGHT_GRAY, 4, true);//文本区域边框设置 Wb = new JTextArea(2, 7); //计算区域 @@ -112,14 +111,31 @@ public class Caculator extends JFrame { Wb.setBorder(border); String [][] str2 = { {"MC", "MR", "MS", "M+", "M-"}, + {"^2","^y","sin","cos","tan"}, + {"10^x","log","lg","ln","e"}, {"←", "CE", "C", "±", "√"}, {"7", "8", "9", "÷", "%"}, {"4", "5", "6", "×", "1/x"}, - {"1", "2", "3", "-", "^2"}, + {"1", "2", "3", "-", "π"}, {"00","0", ".", "+", "="} - };//待修改 - + }; + panel2=new JPanel(); + panel2.setLayout(new GridLayout(8,5)); + for (int i = 0; i < 8; i++) { //插入各个功能、数字按钮 + for (int j = 0; j < 5; j++) { + jbs2[i][j] = new JButton(str2[i][j]); + jbs2[i][j].setFont(new Font("黑体", Font.BOLD, 30)); //设置按钮字体、样式、大小 + panel2.add(jbs2[i][j]); + } + } + for(int i = 0,j = 0;j<5;j++){ //将MC,MR,MS,M+,M-按钮的背景色设为亮灰色 + jbs2[i][j].setBackground(Color.LIGHT_GRAY); + } + jbs2[7][4].setBackground(new Color(240,216,159));//将=按钮的背景色设为淡黄色 + add(Wb, BorderLayout.NORTH);//将输出框置于北端 + add(panel2);//将控制面板2添加进窗口中 + Event2(); } public void Event1() { //设置监听器 for (int i = 2; i < 6; i++) { @@ -154,8 +170,42 @@ public class Caculator extends JFrame { jbs1[1][4].addActionListener(new TextListener(jbs1[1][4], Wb));//√ jbs1[5][4].addActionListener(new ResultListener(Wb));//= } + public void Event2(){ + for (int i = 4; i < 8; i++) { + for (int j = 0; j < 5; j++) { + if(i != 7 || j != 4) + //给数字与运算符号添加监听器 + jbs2[i][j].addActionListener(new TextListener(jbs2[i][j], Wb)); + } + } + jbs2[0][0].addActionListener((e -> Mem = "0")); //MC + jbs2[0][1].addActionListener(e -> Wb.setText(Mem)); //MR + jbs2[0][2].addActionListener(e -> { //MS + Mem = Wb.getText(); + Mem = Mem.substring(Mem.indexOf('\n')+1); //去掉第一行字符串,只留下结果(参考) + }); + jbs2[0][3].addActionListener(e -> { //M+ + double x1, x2; + x1 = Double.parseDouble(Mem); + x2 = Double.parseDouble(Wb.getText()); + Mem = "" + (x1 + x2); + }); + jbs2[0][4].addActionListener(e -> { //M- + double x1, x2; + x1 = Double.parseDouble(Mem); + x2 = Double.parseDouble(Wb.getText()); + Mem = "" + (x2 - x1); + }); + jbs2[3][0].addActionListener(new BackListener(Wb));//← + jbs2[3][1].addActionListener(new BackListener(Wb));//CE + jbs2[3][2].addActionListener(new ClearListener(Wb));//C + jbs2[3][3].addActionListener(new TextListener(jbs2[3][3], Wb));//± + jbs2[3][4].addActionListener(new TextListener(jbs2[3][4], Wb));//√ + jbs2[7][4].addActionListener(new ResultListener(Wb));//= + } } + class ResultListener implements ActionListener { //结果监听器 JTextArea Wb; String str; //保存文本区的字符串 -- Gitee From d99dcced4ee171a84951714fe02f04002af3f2fc Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Mon, 6 Jun 2022 01:02:47 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=A2=9E=E8=AE=BE=E4=BA=86=E5=87=A0?= =?UTF-8?q?=E4=B8=AA=E7=A7=91=E5=AD=A6=E8=AE=A1=E7=AE=97=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Caculator.java | 70 +++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java index 71e8130..eaaccca 100644 --- a/src/java2022spring/Caculator.java +++ b/src/java2022spring/Caculator.java @@ -17,7 +17,7 @@ public class Caculator extends JFrame { JButton jbs2[][] = new JButton[8][5]; String Mem="0";//存储区的值,运用于MC等功能键 Caculator(){ - init2(); + init1(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //设置窗口标题 setTitle("计算器"); @@ -44,24 +44,25 @@ public class Caculator extends JFrame { } catch (Exception e) { e.printStackTrace(); }//UI风格(有参考) - /*it1.addActionListener(new ActionListener() { //标准型 + it1.addActionListener(new ActionListener() { //标准型 public void actionPerformed(ActionEvent e) { - remove(panel1); - panel1 = FirstPanel.firstView(); + panel2.setVisible(false); + panel2.removeAll(); init1(); } }); it2.addActionListener(new ActionListener() { //科学型 public void actionPerformed(ActionEvent e) { - remove(panel2); - panel2 = SecondPanel.secondView(); + panel1.setVisible(false); + panel1.removeAll(); + init2(); } - });*/ + }); setVisible(true);//窗口可见 setLocationRelativeTo(null); //窗口居中 validate(); } - void init1(){ //标准型 + public void init1(){ //标准型 //窗口大小 setSize(450,550); //布局样式 @@ -102,7 +103,7 @@ public class Caculator extends JFrame { } - void init2() { //科学型 + public void init2() { //科学型 setSize(520,650); setLayout(new BorderLayout()); LineBorder border = new LineBorder(Color.LIGHT_GRAY, 4, true);//文本区域边框设置 @@ -112,7 +113,7 @@ public class Caculator extends JFrame { String [][] str2 = { {"MC", "MR", "MS", "M+", "M-"}, {"^2","^y","sin","cos","tan"}, - {"10^x","log","lg","ln","e"}, + {"10^x","log","lg","e","|x|"}, {"←", "CE", "C", "±", "√"}, {"7", "8", "9", "÷", "%"}, {"4", "5", "6", "×", "1/x"}, @@ -178,6 +179,11 @@ public class Caculator extends JFrame { jbs2[i][j].addActionListener(new TextListener(jbs2[i][j], Wb)); } } + for (int i = 1; i < 3; i++) { + for(int j = 0; j < 5; j++) { + jbs2[i][j].addActionListener(new TextListener(jbs2[i][j],Wb)); + } + } jbs2[0][0].addActionListener((e -> Mem = "0")); //MC jbs2[0][1].addActionListener(e -> Wb.setText(Mem)); //MR jbs2[0][2].addActionListener(e -> { //MS @@ -268,7 +274,7 @@ class ResultListener implements ActionListener { // Wb.setText(str + "\n" + x1/100); } - else if (str.contains("-")) { //减法运算(参考) + else if (str.contains("-")&&str.contains("|x|") == false) { //减法运算(参考) if(str.charAt(0) == '-') { //如果第一个字符是“-”,形如-2-3形式,就转化成为-(2+3)形式 String str_x = str; str = str.substring(1); @@ -285,7 +291,47 @@ class ResultListener implements ActionListener { // double x2 = Double.parseDouble(str2); Wb.setText(str + "\n" + (x1-x2)); } - } + } + else if(str.contains("10^x")) { + + } + else if(str.contains("log")) { + + } + else if(str.contains("lg")) { + + } + else if(str.contains("e")) { + + } + else if(str.contains("|x|")) { + String str1 = str.substring(0, str.indexOf("|x|")); + if(str.charAt(0) == '-') { //如果第一个字符是“-” + String str2 = str.substring(1,str.indexOf("|x|")); + double x2 = Double.parseDouble(str2); + Wb.setText(str + "\n" + Math.abs(x2)); + } + else { + double x1 = Double.parseDouble(str1); + Wb.setText(str + "\n" + Math.abs(x1)); + } + } + else if(str.contains("^y")) { + String str1 = str.substring(0,str.indexOf("^y")); + String str2 = str.substring(str.indexOf("^y") + 1, str.indexOf('=')); + double x = Double.parseDouble(str1); + double y = Double.parseDouble(str2); + Wb.setText(str + "\n" + Math.pow(x,y)); + } + else if(str.contains("sin")) { + + } + else if(str.contains("cos")) { + + } + else if(str.contains("tan")) { + + } } } class ClearListener implements ActionListener { //清除监听器,用于C -- Gitee From 74922a84df78964812cfc53d3ba679c9d71e37f3 Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Mon, 6 Jun 2022 21:55:00 +0800 Subject: [PATCH 07/11] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=AE=8C=E7=A7=91?= =?UTF-8?q?=E5=AD=A6=E8=AE=A1=E7=AE=97=E5=99=A8=E7=9A=84=E7=9B=91=E5=90=AC?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Caculator.java | 48 +++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java index eaaccca..3f9b53d 100644 --- a/src/java2022spring/Caculator.java +++ b/src/java2022spring/Caculator.java @@ -113,7 +113,7 @@ public class Caculator extends JFrame { String [][] str2 = { {"MC", "MR", "MS", "M+", "M-"}, {"^2","^y","sin","cos","tan"}, - {"10^x","log","lg","e","|x|"}, + {"X!","ln","lg","e^x","|x|"}, {"←", "CE", "C", "±", "√"}, {"7", "8", "9", "÷", "%"}, {"4", "5", "6", "×", "1/x"}, @@ -251,7 +251,7 @@ class ResultListener implements ActionListener { // else Wb.setText(str + "\n" + x1 / x2); } else if (str.contains("^2")) { //平方运算 - String str1 = str.substring(0, str.indexOf('^')); + String str1 = str.substring(0, str.indexOf("^2")); double x = Double.parseDouble(str1); Wb.setText(str + "\n" + x * x); } @@ -292,17 +292,31 @@ class ResultListener implements ActionListener { // Wb.setText(str + "\n" + (x1-x2)); } } - else if(str.contains("10^x")) { - + else if(str.contains("X!")) { + String str1 = str.substring(str.indexOf('x'),str.indexOf('=')); + double x1 = Double.parseDouble(str1); + int a; + double b=x1 ; + int c=1; + for(a=1;a<=b;a++){ + c=c*a; + x1=c;} + Wb.setText(str + "\n" + x1); } - else if(str.contains("log")) { - + else if(str.contains("ln")) { + String str1 = str.substring(str.indexOf('n')+1, str.indexOf('=')); + double x1 = Double.parseDouble(str1); + Wb.setText(str + "\n" + Math.log(x1)); } else if(str.contains("lg")) { - + String str1 = str.substring(str.indexOf('g')+1,str.indexOf('=')); + double x1 = Double.parseDouble(str1); + Wb.setText(str + "\n" + Math.log10(x1)); } - else if(str.contains("e")) { - + else if(str.contains("e^x")) { + String str1 = str.substring(str.indexOf('x')+1 ,str.indexOf('=')); + double x1 = Double.parseDouble(str1); + Wb.setText(str + "\n" + Math.exp(x1)); } else if(str.contains("|x|")) { String str1 = str.substring(0, str.indexOf("|x|")); @@ -318,19 +332,29 @@ class ResultListener implements ActionListener { // } else if(str.contains("^y")) { String str1 = str.substring(0,str.indexOf("^y")); - String str2 = str.substring(str.indexOf("^y") + 1, str.indexOf('=')); + String str2 = str.substring(str.indexOf('y') + 1, str.indexOf('=')); double x = Double.parseDouble(str1); double y = Double.parseDouble(str2); Wb.setText(str + "\n" + Math.pow(x,y)); } else if(str.contains("sin")) { + String str1 = str.substring(str.indexOf('s') + 3,str.indexOf('=')); + double x1 = Double.parseDouble(str1); + Wb.setText(str + "\n" + Math.sin(x1)); } else if(str.contains("cos")) { - + String str1 = str.substring(str.indexOf('c') + 3,str.indexOf('=')); + double x1 = Double.parseDouble(str1); + Wb.setText(str + "\n" + Math.cos(x1)); } else if(str.contains("tan")) { - + String str1 = str.substring(str.indexOf('t') + 3,str.indexOf('=')); + double x1 = Double.parseDouble(str1); + Wb.setText(str + "\n" + Math.tan(x1)); + } + else if(str.contains("π")) { + Wb.setText(String.valueOf(3.14159265)); } } } -- Gitee From 3a0403600ff8186910c4b73c40feb83d9137ca23 Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Mon, 6 Jun 2022 22:13:45 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86X!=E7=9A=84?= =?UTF-8?q?=E7=9B=91=E5=90=AC=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Caculator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java index 3f9b53d..b6a3041 100644 --- a/src/java2022spring/Caculator.java +++ b/src/java2022spring/Caculator.java @@ -293,7 +293,7 @@ class ResultListener implements ActionListener { // } } else if(str.contains("X!")) { - String str1 = str.substring(str.indexOf('x'),str.indexOf('=')); + String str1 = str.substring(0,str.indexOf('X')); double x1 = Double.parseDouble(str1); int a; double b=x1 ; -- Gitee From b96117bb407ef5027e8dee6aebb3a3d3e1a11493 Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Mon, 6 Jun 2022 23:22:53 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E8=BF=90?= =?UTF-8?q?=E7=AE=97=E7=AC=A6=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Caculator.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java index b6a3041..5acb83c 100644 --- a/src/java2022spring/Caculator.java +++ b/src/java2022spring/Caculator.java @@ -275,7 +275,7 @@ class ResultListener implements ActionListener { // } else if (str.contains("-")&&str.contains("|x|") == false) { //减法运算(参考) - if(str.charAt(0) == '-') { //如果第一个字符是“-”,形如-2-3形式,就转化成为-(2+3)形式 + if(str.charAt(0) == '-') { String str_x = str; str = str.substring(1); String str1 = str.substring(0, str.indexOf('-')); @@ -292,7 +292,7 @@ class ResultListener implements ActionListener { // Wb.setText(str + "\n" + (x1-x2)); } } - else if(str.contains("X!")) { + else if(str.contains("X!")) { //阶乘运算 String str1 = str.substring(0,str.indexOf('X')); double x1 = Double.parseDouble(str1); int a; @@ -303,22 +303,22 @@ class ResultListener implements ActionListener { // x1=c;} Wb.setText(str + "\n" + x1); } - else if(str.contains("ln")) { + else if(str.contains("ln")) { //求自然对数 String str1 = str.substring(str.indexOf('n')+1, str.indexOf('=')); double x1 = Double.parseDouble(str1); Wb.setText(str + "\n" + Math.log(x1)); } - else if(str.contains("lg")) { + else if(str.contains("lg")) { //求以10为底的对数 String str1 = str.substring(str.indexOf('g')+1,str.indexOf('=')); double x1 = Double.parseDouble(str1); Wb.setText(str + "\n" + Math.log10(x1)); } - else if(str.contains("e^x")) { + else if(str.contains("e^x")) { //求自然常数的x次方 String str1 = str.substring(str.indexOf('x')+1 ,str.indexOf('=')); double x1 = Double.parseDouble(str1); Wb.setText(str + "\n" + Math.exp(x1)); } - else if(str.contains("|x|")) { + else if(str.contains("|x|")) { //求绝对值 String str1 = str.substring(0, str.indexOf("|x|")); if(str.charAt(0) == '-') { //如果第一个字符是“-” String str2 = str.substring(1,str.indexOf("|x|")); @@ -330,30 +330,30 @@ class ResultListener implements ActionListener { // Wb.setText(str + "\n" + Math.abs(x1)); } } - else if(str.contains("^y")) { + else if(str.contains("^y")) { //求x的y次方 String str1 = str.substring(0,str.indexOf("^y")); String str2 = str.substring(str.indexOf('y') + 1, str.indexOf('=')); double x = Double.parseDouble(str1); double y = Double.parseDouble(str2); Wb.setText(str + "\n" + Math.pow(x,y)); } - else if(str.contains("sin")) { + else if(str.contains("sin")) { //正弦 String str1 = str.substring(str.indexOf('s') + 3,str.indexOf('=')); double x1 = Double.parseDouble(str1); Wb.setText(str + "\n" + Math.sin(x1)); } - else if(str.contains("cos")) { + else if(str.contains("cos")) { //余弦 String str1 = str.substring(str.indexOf('c') + 3,str.indexOf('=')); double x1 = Double.parseDouble(str1); Wb.setText(str + "\n" + Math.cos(x1)); } - else if(str.contains("tan")) { + else if(str.contains("tan")) { //正切 String str1 = str.substring(str.indexOf('t') + 3,str.indexOf('=')); double x1 = Double.parseDouble(str1); Wb.setText(str + "\n" + Math.tan(x1)); } - else if(str.contains("π")) { + else if(str.contains("π")) { //PI Wb.setText(String.valueOf(3.14159265)); } } -- Gitee From 3949254394fef9be1203f33b22a62edb30618b61 Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Mon, 6 Jun 2022 23:33:56 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E6=A0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Caculator.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java index 5acb83c..50f585d 100644 --- a/src/java2022spring/Caculator.java +++ b/src/java2022spring/Caculator.java @@ -11,7 +11,7 @@ public class Caculator extends JFrame { JMenuBar jMenuBar; JPanel panel1,panel2;//控制面板 JMenu jm1,jm2,jm3; - JMenuItem it1,it2,it3; + JMenuItem it1,it2,it3,it4,it5,it6,it7; JTextArea Wb;//文本区 JButton jbs1[][] = new JButton[6][5];//有参考 JButton jbs2[][] = new JButton[8][5]; @@ -34,10 +34,18 @@ public class Caculator extends JFrame { jMenuBar.add(jm3); it1 = new JMenuItem("标准"); it2 = new JMenuItem("科学"); - it3 = new JMenuItem("作者:zxy"); + it3 = new JMenuItem("Hello!"); + it4 = new JMenuItem("复制(C)"); + it5 = new JMenuItem("粘贴(P)"); + it6 = new JMenuItem("关于 计算器"); + it7 = new JMenuItem("作者:zxy"); jm1.add(it1); jm1.add(it2); jm3.add(it3); + jm2.add(it4); + jm2.add(it5); + jm3.add(it6); + jm3.add(it7); this.setJMenuBar(jMenuBar); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); -- Gitee From 557fbf0d96c301fc193b86796260b7938f652f6d Mon Sep 17 00:00:00 2001 From: 12636 <12636@LAPTOP-0OEL8GL6> Date: Thu, 9 Jun 2022 22:05:56 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java2022spring/Caculator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java index 50f585d..28d323b 100644 --- a/src/java2022spring/Caculator.java +++ b/src/java2022spring/Caculator.java @@ -276,7 +276,7 @@ class ResultListener implements ActionListener { // if (x2 == 0) Wb.setText(str + "\n" + x1); else Wb.setText(str + "\n" + (x1 + x2) + " & " + (x1 - x2)); } - else if (str.contains("%")) { //百分号运算 + else if (str.contains("%")) { //取余运算 String str1 = str.substring(0, str.indexOf('%')); double x1 = Double.parseDouble(str1); Wb.setText(str + "\n" + x1/100); -- Gitee