diff --git a/src/java2022spring/Caculator.java b/src/java2022spring/Caculator.java new file mode 100644 index 0000000000000000000000000000000000000000..28d323b956964ffa826b5ad8b6d8c172b92fd0b4 --- /dev/null +++ b/src/java2022spring/Caculator.java @@ -0,0 +1,404 @@ +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 panel1,panel2;//控制面板 + JMenu jm1,jm2,jm3; + JMenuItem it1,it2,it3,it4,it5,it6,it7; + JTextArea Wb;//文本区 + JButton jbs1[][] = new JButton[6][5];//有参考 + JButton jbs2[][] = new JButton[8][5]; + String Mem="0";//存储区的值,运用于MC等功能键 + Caculator(){ + init1(); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + //设置窗口标题 + setTitle("计算器"); + //菜单栏 + 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("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"); + } catch (Exception e) { + e.printStackTrace(); + }//UI风格(有参考) + it1.addActionListener(new ActionListener() { //标准型 + public void actionPerformed(ActionEvent e) { + panel2.setVisible(false); + panel2.removeAll(); + init1(); + } + }); + it2.addActionListener(new ActionListener() { //科学型 + public void actionPerformed(ActionEvent e) { + panel1.setVisible(false); + panel1.removeAll(); + init2(); + } + }); + setVisible(true);//窗口可见 + setLocationRelativeTo(null); //窗口居中 + validate(); + } + public void init1(){ //标准型 + //窗口大小 + setSize(450,550); + //布局样式 + 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 [][] str1 = { + {"MC", "MR", "MS", "M+", "M-"}, + {"←", "CE", "C", "±", "√"}, + {"7", "8", "9", "÷", "%"}, + {"4", "5", "6", "×", "1/x"}, + {"1", "2", "3", "-", "^2"}, + {"00","0", ".", "+", "="} + };//有参考 + panel1 = new JPanel(); + panel1.setLayout(new GridLayout(6, 5));//设置容器为网格布局 + for (int i = 0; i < 6; i++) { //插入各个功能、数字按钮 + for (int j = 0; j < 5; 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-按钮的背景色设为亮灰色 + jbs1[i][j].setBackground(Color.LIGHT_GRAY); + } + jbs1[5][4].setBackground(new Color(240,216,159));//将=按钮的背景色设为淡黄色 + add(Wb, BorderLayout.NORTH);//将输出框置于北端 + add(panel1);//将控制面板1添加进窗口中 + + Event1(); + + } + + public void init2() { //科学型 + setSize(520,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-"}, + {"^2","^y","sin","cos","tan"}, + {"X!","ln","lg","e^x","|x|"}, + {"←", "CE", "C", "±", "√"}, + {"7", "8", "9", "÷", "%"}, + {"4", "5", "6", "×", "1/x"}, + {"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++) { + for (int j = 0; j < 5; j++) { + if(i != 5 || j != 4) + //给数字与运算符号添加监听器 + jbs1[i][j].addActionListener(new TextListener(jbs1[i][j], Wb)); + } + } + 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); //去掉第一行字符串,只留下结果(参考) + }); + jbs1[0][3].addActionListener(e -> { //M+ + double x1, x2; + x1 = Double.parseDouble(Mem); + x2 = Double.parseDouble(Wb.getText()); + Mem = "" + (x1 + x2); + }); + jbs1[0][4].addActionListener(e -> { //M- + double x1, x2; + x1 = Double.parseDouble(Mem); + x2 = Double.parseDouble(Wb.getText()); + Mem = "" + (x2 - x1); + }); + 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));//= + } + 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)); + } + } + 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 + 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; //保存文本区的字符串 + public ResultListener(JTextArea Wb1) { //将文本区传入 + Wb = Wb1; + } + public void actionPerformed(ActionEvent e) { + 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); + 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); + 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) 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) Wb.setText(str + "\n" + "不能除以0"); //错误处理 + else Wb.setText(str + "\n" + x1 / x2); + } + else if (str.contains("^2")) { //平方运算 + String str1 = str.substring(0, str.indexOf("^2")); + double x = Double.parseDouble(str1); + Wb.setText(str + "\n" + x * x); + } + else if (str.contains("√")) { //开方运算 + String str1 = str.substring(str.indexOf('√') + 1, str.indexOf('=')); + double x = Double.parseDouble(str1); + 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) 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); + Wb.setText(str + "\n" + x1/100); + + } + else if (str.contains("-")&&str.contains("|x|") == false) { //减法运算(参考) + if(str.charAt(0) == '-') { + 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); + 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); + Wb.setText(str + "\n" + (x1-x2)); + } + } + else if(str.contains("X!")) { //阶乘运算 + String str1 = str.substring(0,str.indexOf('X')); + 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("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")) { //求以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")) { //求自然常数的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|")); + 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")) { //求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")) { //正弦 + 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("π")) { //PI + Wb.setText(String.valueOf(3.14159265)); + } + } +} +class ClearListener implements ActionListener { //清除监听器,用于C + JTextArea Wb; + public ClearListener(JTextArea textArea1) { //将按钮和文本区传入 + Wb = textArea1; + } + public void actionPerformed(ActionEvent e) { //设置文本区内容为空,即清空 + Wb.setText(""); + } +} + +class BackListener implements ActionListener { //用于←,CE + JTextArea Wb; + String str; + public BackListener(JTextArea Wb1) { //将文本区传入 + Wb = Wb1; + } + public void actionPerformed(ActionEvent e) { //设置文本区 + str = Wb.getText(); + if(str.length() == 0) Wb.setText(""); //如果文本区内容为空,输出为空 + else { //否则让当前文本区删掉最后一个符号/数字 + str = str.substring(0, str.length()-1); + Wb.setText(str); + } + } +} +class TextListener implements ActionListener { //文本监听器 + JButton button; + JTextArea Wb; + public TextListener(JButton button1, JTextArea Wb1) { //将按钮和文本区传入 + Wb = Wb1; + button = button1; + } + public void actionPerformed(ActionEvent e) { //设置文本区 + Wb.setText(Wb.getText() + button.getText()); + } +} \ No newline at end of file diff --git a/src/java2022spring/Test.java b/src/java2022spring/Test.java index 24deb29bfc0e5f50fdedcd21b504e77b529d48b6..b848e89cd0b0b55da04823b5eb4c65c68f1bfa3d 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(); + } }