diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000000000000000000000000000000000..26438fe82384544487f2191aef49d4b0ef346eb7 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/README.md=UTF-8 diff --git a/src/java2020spring/CalcFrame.java b/src/java2020spring/CalcFrame.java new file mode 100644 index 0000000000000000000000000000000000000000..1ff7e01160487bd27fb9c39ce6536a916dde08b5 --- /dev/null +++ b/src/java2020spring/CalcFrame.java @@ -0,0 +1,11 @@ +package java2020spring; + +import java.awt.*; +import javax.swing.*; +public class CalcFrame { + public static void main(String args[]) { + WindowActionEvent win=new WindowActionEvent(); + win.setTitle("科学计算器");//标题 + win.setBounds(400,150,790,680);//大小 + } +} \ No newline at end of file diff --git a/src/java2020spring/WindowActionEvent.java b/src/java2020spring/WindowActionEvent.java new file mode 100644 index 0000000000000000000000000000000000000000..a236e45c5da3a1700a55efe474fc6f4547da5955 --- /dev/null +++ b/src/java2020spring/WindowActionEvent.java @@ -0,0 +1,617 @@ +package java2020spring; + +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.*; +import javax.swing.border.LineBorder; +public class WindowActionEvent extends JFrame { + String operator="=";//记录运算符 + boolean ifResult=true,flag=false;//判断是否有结果 + double result=0;//定义计算结果 + JTextField showView;//文本框定义窗口显示框 + //OperatorActionListen operatorAction=new OperatorActionListen();//运算符处理监视器 + NumberActionListen numberAction=new NumberActionListen();//数字处理监视器 + PointActionListen pointAction=new PointActionListen();//小数点监视器 + SignActionListen signAction=new SignActionListen();//特殊运算符监视器 + ClearActionListen clearAction=new ClearActionListen();//清除显示框监视器 + + //窗口初始化操作 + public WindowActionEvent() { + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭方式 + setResizable(false);//不可改变窗口大小 + setVisible(true);//窗口可见 + setLayout(null);//空布局 + + //窗口内添加计算器的显示框,用来显示输入数据和输出结果 + showView = new JTextField(10); + showView.setBounds(15, 10, 745, 120);//显示框大小 + showView.setEditable(false);//显示框只读 + showView.setHorizontalAlignment(JTextField.RIGHT);//文字右对齐 + showView.setBackground(Color.white);//设置背景色 + showView.setFont(new Font("Cambria", Font.BOLD, 24));//设置字体 粗细 大小 + add(showView); + + //添加按钮及相应的按钮初始化操作 + JButton button_leftbracket1 = new JButton("(");//添加左括号 + button_leftbracket1.setBounds(40,170,80,60);//设置按钮分布,大小 + button_leftbracket1.setBorder(new LineBorder(Color.lightGray,1));//设置按钮边框颜色 + button_leftbracket1.setFont(new Font("黑体", Font.PLAIN, 24));//设置字体 粗细 大小 + button_leftbracket1.setBackground(Color.pink);//设置按钮背景颜色 + button_leftbracket1.addActionListener(signAction);//添加事件处理 + add(button_leftbracket1);//添加按钮 + + JButton button_leftbracket2 = new JButton(")");//添加右括号 + button_leftbracket2.setBounds(140,170,80,60); + button_leftbracket2.setBorder(new LineBorder(Color.lightGray,1)); + button_leftbracket2.setFont(new Font("黑体", Font.PLAIN, 24)); + button_leftbracket2.setBackground(Color.pink); + button_leftbracket2.addActionListener(signAction); + add(button_leftbracket2); + + JButton button_reciprocal = new JButton("1/x");//添加求倒数键 + button_reciprocal.setBounds(240,170,80,60); + button_reciprocal.setBorder(new LineBorder(Color.lightGray,1)); + button_reciprocal.setFont(new Font("宋体", Font.PLAIN, 22)); + button_reciprocal.setBackground(Color.orange); + button_reciprocal.addActionListener(signAction); + add(button_reciprocal); + + JButton button_mc = new JButton("mc");//添加mc键,用于清零之前存储的数字 + button_mc.setBounds(340,170,80,60); + button_mc.setBorder(new LineBorder(Color.lightGray,1)); + button_mc.setFont(new Font("黑体", Font.PLAIN, 24)); + button_mc.setBackground(Color.gray); + //button_mc.addActionListener(); + add(button_mc); + + JButton button_m1 = new JButton("m+");//添加m+键,存储内加上数字 + button_m1.setBounds(440,170,80,60); + button_m1.setBorder(new LineBorder(Color.lightGray,1)); + button_m1.setFont(new Font("黑体", Font.PLAIN, 24)); + button_m1.setBackground(Color.gray); + //button_m1.addActionListener(); + add(button_m1); + + JButton button_m2 = new JButton("m-");//添加m-键,存储内减去数字 + button_m2.setBounds(540,170,80,60); + button_m2.setBorder(new LineBorder(Color.lightGray,1)); + button_m2.setFont(new Font("黑体", Font.PLAIN, 24)); + button_m2.setBackground(Color.gray); + //button_m2.addActionListener(); + add(button_m2); + + JButton button_mr = new JButton("mr");//添加mr键,用于存储数字 + button_mr.setBounds(640,170,80,60); + button_mr.setBorder(new LineBorder(Color.lightGray,1)); + button_mr.setFont(new Font("黑体", Font.PLAIN, 24)); + button_mr.setBackground(Color.gray); + //button_mr.addActionListener(); + add(button_mr); + + JButton button_square = new JButton("x^2");//添加平方计算 + button_square.setBounds(40,250,80,60); + button_square.setBorder(new LineBorder(Color.lightGray,1)); + button_square.setFont(new Font("宋体", Font.PLAIN, 22)); + button_square.setBackground(Color.orange); + button_square.addActionListener(signAction); + add(button_square); + + JButton button_factorial = new JButton("x!");//添加阶乘计算 + button_factorial.setBounds(40,330,80,60); + button_factorial.setBorder(new LineBorder(Color.lightGray,1)); + button_factorial.setFont(new Font("宋体", Font.PLAIN, 22)); + button_factorial.setBackground(Color.orange); + button_factorial.addActionListener(signAction); + add(button_factorial); + + JButton button_e = new JButton("e");//添加自然常数e + button_e.setBounds(40,410,80,60); + button_e.setBorder(new LineBorder(Color.lightGray,1)); + button_e.setFont(new Font("宋体", Font.PLAIN, 22)); + button_e.setBackground(Color.pink); + button_e.addActionListener(numberAction); + add(button_e); + + JButton button_sin = new JButton("sin");//添加三角函数sin + button_sin.setBounds(40,490,80,30); + button_sin.setBorder(new LineBorder(Color.lightGray,1)); + button_sin.setFont(new Font("宋体", Font.PLAIN, 22)); + button_sin.setBackground(Color.green); + button_sin.addActionListener(signAction); + add(button_sin); + + JButton button_arcsin = new JButton("arcsin");//添加反三角函数arcsin + button_arcsin.setBounds(40,530,80,30); + button_arcsin.setBorder(new LineBorder(Color.lightGray,1)); + button_arcsin.setFont(new Font("宋体", Font.PLAIN, 22)); + button_arcsin.setBackground(Color.green); + button_arcsin.addActionListener(signAction); + add(button_arcsin); + + JButton button_lnv = new JButton("lnv");//添加lnv键,用于改变sin,cos,tan改为对应的反函数的值 + button_lnv.setBounds(40,570,80,60); + button_lnv.setBorder(new LineBorder(Color.lightGray,1)); + button_lnv.setFont(new Font("宋体", Font.PLAIN, 22)); + button_lnv.setBackground(Color.green); + //button_lnv.addActionListener(); + add(button_lnv); + + JButton button_thirdPower = new JButton("x^3");//添加立方计算 + button_thirdPower.setBounds(140,250,80,60); + button_thirdPower.setBorder(new LineBorder(Color.lightGray,1)); + button_thirdPower.setFont(new Font("宋体", Font.PLAIN, 22)); + button_thirdPower.setBackground(Color.orange); + button_thirdPower.addActionListener(signAction); + add(button_thirdPower); + + JButton button_radical1 = new JButton("√");//添加开平方计算 + button_radical1.setBounds(140,330,80,60); + button_radical1.setBorder(new LineBorder(Color.lightGray,1)); + button_radical1.setFont(new Font("宋体", Font.PLAIN, 22)); + button_radical1.setBackground(Color.orange); + button_radical1.addActionListener(signAction); + add(button_radical1); + + JButton button_ln = new JButton("ln");//添加对数ln计算 + button_ln.setBounds(140,410,80,60); + button_ln.setBorder(new LineBorder(Color.lightGray,1)); + button_ln.setFont(new Font("宋体", Font.PLAIN, 22)); + button_ln.setBackground(Color.orange); + button_ln.addActionListener(signAction); + add(button_ln); + + JButton button_cos = new JButton("cos");//添加三角函数cos + button_cos.setBounds(140,490,80,30); + button_cos.setBorder(new LineBorder(Color.lightGray,1)); + button_cos.setFont(new Font("宋体", Font.PLAIN, 22)); + button_cos.setBackground(Color.green); + button_cos.addActionListener(signAction); + add(button_cos); + + JButton button_arccos = new JButton("arccos");//添加反三角函数arccos + button_arccos.setBounds(140,530,80,30); + button_arccos.setBorder(new LineBorder(Color.lightGray,1)); + button_arccos.setFont(new Font("宋体", Font.PLAIN, 22)); + button_arccos.setBackground(Color.green); + button_arccos.addActionListener(signAction); + add(button_arccos); + + JButton button_Deg = new JButton("Deg");//添加角度制计算方式 + button_Deg.setBounds(140,570,80,60); + button_Deg.setBorder(new LineBorder(Color.lightGray,1)); + button_Deg.setFont(new Font("宋体", Font.PLAIN, 22)); + button_Deg.setBackground(Color.yellow); + button_Deg.addActionListener(signAction); + add(button_Deg); + + JButton button_Power = new JButton("x^y");//添加指数计算 + button_Power.setBounds(240,250,80,60); + button_Power.setBorder(new LineBorder(Color.lightGray,1)); + button_Power.setFont(new Font("宋体", Font.PLAIN, 22)); + button_Power.setBackground(Color.orange); + button_Power.addActionListener(signAction); + add(button_Power); + + JButton button_radical2 = new JButton("y√x");//添加根号计算 + button_radical2.setBounds(240,330,80,60); + button_radical2.setBorder(new LineBorder(Color.lightGray,1)); + button_radical2.setFont(new Font("宋体", Font.PLAIN, 22)); + button_radical2.setBackground(Color.orange); + button_radical2.addActionListener(signAction); + add(button_radical2); + + JButton button_log = new JButton("logx(y)");//添加对数log计算,以x为底计算y的导数 + button_log.setBounds(240,410,80,60); + button_log.setBorder(new LineBorder(Color.lightGray,1)); + button_log.setFont(new Font("宋体", Font.PLAIN, 22)); + button_log.setBackground(Color.orange); + button_log.addActionListener(signAction); + add(button_log); + + JButton button_tan = new JButton("tan");//添加三角函数tan + button_tan.setBounds(240,490,80,30); + button_tan.setBorder(new LineBorder(Color.lightGray,1)); + button_tan.setFont(new Font("宋体", Font.PLAIN, 22)); + button_tan.setBackground(Color.green); + button_tan.addActionListener(signAction); + add(button_tan); + + JButton button_arctan = new JButton("arctan");//添加三角函数arctan + button_arctan.setBounds(240,530,80,30); + button_arctan.setBorder(new LineBorder(Color.lightGray,1)); + button_arctan.setFont(new Font("宋体", Font.PLAIN, 22)); + button_arctan.setBackground(Color.green); + button_arctan.addActionListener(signAction); + add(button_arctan); + + JButton button_Π = new JButton("Π");//添加圆周率Π + button_Π.setBounds(240,570,80,60); + button_Π.setBorder(new LineBorder(Color.lightGray,1)); + button_Π.setFont(new Font("宋体", Font.PLAIN, 22)); + button_Π.setBackground(Color.pink); + button_Π.addActionListener(numberAction); + add(button_Π); + + JButton button_C = new JButton("C");//添加C键,用于清空显示框的数字 + button_C.setBounds(340,250,80,60); + button_C.setBorder(new LineBorder(Color.lightGray,1)); + button_C.setBackground(Color.green); + button_C.setForeground(Color.blue); + button_C.setFont(new Font("黑体", Font.BOLD, 24)); + button_C.addActionListener(clearAction); + add(button_C); + + JButton button_7 = new JButton("7");//添加数字7 + button_7.setBounds(340,330,80,60); + button_7.setBorder(new LineBorder(Color.lightGray,1)); + button_7.setBackground(Color.red); + button_7.setForeground(Color.black); + button_7.setFont(new Font("黑体", Font.BOLD, 30)); + button_7.addActionListener(numberAction); + add(button_7); + + JButton button_4 = new JButton("4");//添加数字4 + button_4.setBounds(340,410,80,60); + button_4.setBorder(new LineBorder(Color.lightGray,1)); + button_4.setBackground(Color.red); + button_4.setForeground(Color.black); + button_4.setFont(new Font("黑体", Font.BOLD, 30)); + button_4.addActionListener(numberAction); + add(button_4); + + JButton button_1 = new JButton("1");//添加数字1 + button_1.setBounds(340,490,80,60); + button_1.setBorder(new LineBorder(Color.lightGray,1)); + button_1.setBackground(Color.red); + button_1.setForeground(Color.black); + button_1.setFont(new Font("黑体", Font.BOLD, 30)); + button_1.addActionListener(numberAction); + add(button_1); + + JButton button_percentage = new JButton("%");//添加百分号 + button_percentage.setBounds(340,570,80,60); + button_percentage.setBorder(new LineBorder(Color.lightGray,1)); + button_percentage.setFont(new Font("宋体", Font.PLAIN, 24)); + button_percentage.setBackground(Color.CYAN); + button_percentage.setForeground(Color.red); + button_percentage.addActionListener(signAction); + add(button_percentage); + + JButton button_divide = new JButton("÷");//添加除法运算 + button_divide.setBounds(440,250,80,60); + button_divide.setBorder(new LineBorder(Color.lightGray,1)); + button_divide.setBackground(Color.yellow); + button_divide.setForeground(Color.black); + button_divide.setFont(new Font("黑体", Font.BOLD, 30)); + button_divide.addActionListener(signAction); + add(button_divide); + + JButton button_8 = new JButton("8");//添加数字8 + button_8.setBounds(440,330,80,60); + button_8.setBorder(new LineBorder(Color.lightGray,1)); + button_8.setBackground(Color.red); + button_8.setForeground(Color.black); + button_8.setFont(new Font("黑体", Font.BOLD, 30)); + button_8.addActionListener(numberAction); + add(button_8); + + JButton button_5 = new JButton("5");//添加数字5 + button_5.setBounds(440,410,80,60); + button_5.setBorder(new LineBorder(Color.lightGray,1)); + button_5.setBackground(Color.red); + button_5.setForeground(Color.black); + button_5.setFont(new Font("黑体", Font.BOLD, 30)); + button_5.addActionListener(numberAction); + add(button_5); + + JButton button_2 = new JButton("2");//添加数字2 + button_2.setBounds(440,490,80,60); + button_2.setBorder(new LineBorder(Color.lightGray,1)); + button_2.setBackground(Color.red); + button_2.setForeground(Color.black); + button_2.setFont(new Font("黑体", Font.BOLD, 30)); + button_2.addActionListener(numberAction); + add(button_2); + + JButton button_0 = new JButton("0");//添加数字0 + button_0.setBounds(440,570,80,60); + button_0.setBorder(new LineBorder(Color.lightGray,1)); + button_0.setBackground(Color.red); + button_0.setForeground(Color.black); + button_0.setFont(new Font("黑体", Font.BOLD, 30)); + button_0.addActionListener(numberAction); + add(button_0); + + JButton button_multiply = new JButton("×");//添加乘法运算 + button_multiply.setBounds(540,250,80,60); + button_multiply.setBorder(new LineBorder(Color.lightGray,1)); + button_multiply.setBackground(Color.yellow); + button_multiply.setForeground(Color.black); + button_multiply.setFont(new Font("黑体", Font.BOLD, 30)); + button_multiply.addActionListener(signAction); + add(button_multiply); + + JButton button_9 = new JButton("9");//添加数字9 + button_9.setBounds(540,330,80,60); + button_9.setBorder(new LineBorder(Color.lightGray,1)); + button_9.setBackground(Color.red); + button_9.setForeground(Color.black); + button_9.setFont(new Font("黑体", Font.BOLD, 30)); + button_9.addActionListener(numberAction); + add(button_9); + + JButton button_6 = new JButton("6");//添加数字6 + button_6.setBounds(540,410,80,60); + button_6.setBorder(new LineBorder(Color.lightGray,1)); + button_6.setBackground(Color.red); + button_6.setForeground(Color.black); + button_6.setFont(new Font("黑体", Font.BOLD, 30)); + button_6.addActionListener(numberAction); + add(button_6); + + JButton button_3 = new JButton("3");//添加数字3 + button_3.setBounds(540,490,80,60); + button_3.setBorder(new LineBorder(Color.lightGray,1)); + button_3.setBackground(Color.red); + button_3.setForeground(Color.black); + button_3.setFont(new Font("黑体", Font.BOLD, 30)); + button_3.addActionListener(numberAction); + add(button_3); + + JButton button_point = new JButton(".");//添加小数点 + button_point.setBounds(540,570,80,60); + button_point.setBorder(new LineBorder(Color.lightGray,1)); + button_point.setFont(new Font("宋体", Font.PLAIN, 28)); + button_point.setBackground(Color.CYAN); + button_point.setForeground(Color.red); + button_point.addActionListener(pointAction); + add(button_point); + + JButton button_delete = new JButton("Delete");//添加删除键,用于删除刚输入的数字(退一格) + button_delete.setBounds(640,250,80,60); + button_delete.setBorder(new LineBorder(Color.lightGray,1)); + button_delete.setBackground(Color.black); + button_delete.setForeground(Color.yellow); + button_delete.setFont(new Font("黑体", Font.BOLD, 22)); + button_delete.addActionListener(clearAction); + add(button_delete); + + JButton button_subtract = new JButton("-");//添加减法运算 + button_subtract.setBounds(640,330,80,60); + button_subtract.setBorder(new LineBorder(Color.lightGray,1)); + button_subtract.setBackground(Color.yellow); + button_subtract.setForeground(Color.black); + button_subtract.setFont(new Font("黑体", Font.BOLD, 30)); + button_subtract.addActionListener(signAction); + add(button_subtract); + + JButton button_plus = new JButton("+");//添加加法运算 + button_plus.setBounds(640,410,80,60); + button_plus.setBorder(new LineBorder(Color.lightGray,1)); + button_plus.setBackground(Color.yellow); + button_plus.setForeground(Color.black); + button_plus.setFont(new Font("黑体", Font.BOLD, 30)); + button_plus.addActionListener(signAction); + add(button_plus); + + JButton button_equal = new JButton("=");//添加等号,用于计算结果 + button_equal.setBounds(640,490,80,140); + button_equal.setBorder(new LineBorder(Color.lightGray,1)); + button_equal.setBackground(Color.blue); + button_equal.setForeground(Color.white); + button_equal.setFont(new Font("黑体", Font.BOLD, 30)); + button_equal.addActionListener(signAction); + add(button_equal); + + + } + //数字输入监听器 + class NumberActionListen implements ActionListener { + public void actionPerformed(ActionEvent e) { + String str=e.getActionCommand();//得到数字字符串 + if(ifResult) { + showView.setText(""); + ifResult=false; + } + if(str=="Π") { //添加圆周率 + showView.setText(String.valueOf(Math.PI)); + } + else if(str=="e") { //添加自然常数 + showView.setText(String.valueOf(Math.E)); + } + else { + showView.setText(showView.getText().trim()+str);//显示框显示输入数字 + if(showView.getText().contentEquals("0")) { //防止有效数字出现前出现多个零 + showView.setText("0"); + ifResult=true; + flag=true; + } + } + } + } + + //小数点输入监听器 + class PointActionListen implements ActionListener{ + public void actionPerformed(ActionEvent e) { + ifResult=false; + if(showView.getText().trim().indexOf(".")==-1) {//去两端空格得小数点 + showView.setText(showView.getText()+"."); + } + } + } + + //加减乘除、x^y、y√x、logx(y)操作 + void getResult(double x) { + if(operator=="+") { //定义加法计算方法 + result+=x; + } + else if(operator=="-") { //定义减法计算方法 + result-=x; + } + else if(operator=="×") { //定义乘法计算方法 + result*=x; + } + else if(operator=="÷") { //定义除法计算方法 + result/=x; + } + else if(operator=="=") { //定义等号结果计算方法 + result=x; + } + else if(operator=="x^y") { //定义x的y次方计算方法 + result=Math.pow(result, x); + } + else if(operator=="y√x") { //定义x的开y次方根计算方法 + result=Math.pow(result,1d/x); + } + else if(operator=="logx(y)") { //定义以x为底y的对数计算方法 + if(x>0) { + result=Math.log(x)/Math.log(result); + } + else { + showView.setText("出错"); + } + } + String strresult=String.valueOf(result); + showView.setText(strresult); //显示结果 + } + + //运算符号的监听器 + class SignActionListen implements ActionListener{ + public void actionPerformed(ActionEvent e) { + String str=e.getActionCommand(); + if(str.equals("(")) { //添加左括号事件处理 + showView.setText("("); + } + else if(str.equals(")")) { //添加右括号事件处理 + showView.setText(")"); + } + else if(str.equals("1/x")) { //添加倒数事件处理 + if(Double.parseDouble(showView.getText())==0) { + showView.setText("出错"); + } + else { + showView.setText(String.valueOf(1/Double.parseDouble(showView.getText()))); + } + } + else if(str.equals("x^2")) { //添加平方事件处理 + double i=Double.parseDouble(showView.getText()); + showView.setText(String.valueOf(i*i)); + } + else if(str.equals("x^3")) { //添加立方事件处理 + double i=Double.parseDouble(showView.getText()); + showView.setText(String.valueOf(i*i*i)); + } + else if(str.equals("x!")) { //添加阶乘事件处理 + double i=Double.parseDouble(showView.getText()); + if((i%2==0)||(i%2==1)) { + int j=(int) i ; + int result=1; + for(int k=1;k<=j;k++) + result *=k; + showView.setText(String.valueOf(result)); + } + else { + showView.setText("出错"); + } + } + else if(str.equals("√")) { //添加根号事件处理 + double i=Double.parseDouble(showView.getText()); + if(i>=0) { + showView.setText(String.valueOf(Math.sqrt(i))); + } + else { + showView.setText("出错"); + } + } + else if(str.equals("ln")) { //添加以e为底求对数事件处理 + double i=Double.parseDouble(showView.getText()); + if(i>0) { + showView.setText(String.valueOf(Math.log(i))); + } + else { + showView.setText("出错"); + } + } + else if(str.equals("sin")) { //添加三角函数sin事件处理 + double i=Double.parseDouble(showView.getText()); + showView.setText(String.valueOf(Math.sin(i))); + } + else if(str.equals("cos")) { //添加三角函数cos事件处理 + double i=Double.parseDouble(showView.getText()); + showView.setText(String.valueOf(Math.cos(i))); + } + else if(str.equals("tan")) { //添加三角函数tan事件处理 + double i=Double.parseDouble(showView.getText()); + showView.setText(String.valueOf(Math.tan(i))); + } + else if(str.equals("%")) { //添加百分号事件处理 + showView.setText(String.valueOf(Double.parseDouble(showView.getText())/100)); + } + else if(str.equals("Deg")) { //添加角度制事件处理 + double i=Double.parseDouble(showView.getText()); + showView.setText(String.valueOf(i/180*Math.PI)); + } + /*else if(str.equals("lnv")) { //求对应三角函数的反函数结果 + };*/ + else if(str.equals("arcsin")) { //添加三角函数arcsin事件处理 + double i=Double.parseDouble(showView.getText()); + showView.setText(String.valueOf(Math.asin(i))); + } + else if(str.equals("arccos")) { //添加三角函数arccos事件处理 + double i=Double.parseDouble(showView.getText()); + showView.setText(String.valueOf(Math.acos(i))); + } + else if(str.equals("arctan")) { //添加三角函数arctan事件处理 + double i=Double.parseDouble(showView.getText()); + showView.setText(String.valueOf(Math.atan(i))); + } + else { //执行加减乘除、x^y、y√x、logx(y)操作 + if(flag) { + ifResult=false; + } + if(ifResult) { + operator=str; + } + else { + getResult(Double.parseDouble(showView.getText())); + operator=str; + ifResult=true; + } + } + } + } + + //清除显示框事件监视器 + class ClearActionListen implements ActionListener{ + public void actionPerformed(ActionEvent e) { + String str=e.getActionCommand(); + if(str=="C") { //添加C清除键事件处理 + showView.setText("0"); + ifResult=true; + result=0; + } + else if(str=="Delete") { //添加Delete删除键事件处理 + if(Double.parseDouble(showView.getText())>0) { + if(showView.getText().length()>1) { + showView.setText(showView.getText().substring(0,showView.getText().length()-1)); + } + else { + showView.setText("0"); + ifResult=true; + } + } + else { + if(showView.getText().length()>2) { + showView.setText(showView.getText().substring(0,showView.getText().length()-1)); + } + else { + showView.setText("0"); + ifResult=true; + } + } + } + } + } +}