⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commandprocessor.java

📁 使用eclipse编写的计算器程序
💻 JAVA
字号:
package mine;


import java.awt.event.*;

public class commandProcessor implements ActionListener//创建计算器事件响应类
{
		String cmd;							//获取按钮命令字符串
		CalculatorPanel myPanel;					//监听器相应的CalculatorPanel对象
		public commandProcessor(CalculatorPanel myPanel)//构造方法获取CalculatorPanel对象
		{
			this.myPanel=myPanel;
			
		}		
		public void actionPerformed(ActionEvent e)
		{
			cmd=e.getActionCommand();		//获取命令
			if(cmd=="1"|cmd=="2"|cmd=="3"|cmd=="4"|cmd=="5"|cmd=="6"|cmd=="7"|cmd=="8"|cmd=="9"|cmd=="0")//数字键响应
			{
				if(myPanel.flag) 				//若当可更新text内容,则在原基础上添加该数字
				{
					myPanel.s+=cmd;
					if(myPanel.containdot==false)//若不是小数,操作字符串后加小数点显示出来,否则直接显示当前操作数
						myPanel.resultField.setText(myPanel.s+".");
					else myPanel.resultField.setText(myPanel.s);
						myPanel.enterednum=true;
				}
				else 						//否则重新设置操作字符串为该数字
				{
					myPanel.s="";					
					myPanel.flag=true;
					myPanel.s=cmd;
					myPanel.resultField.setText(myPanel.s+".");										
				}
			}
			else if(cmd=="+"|cmd=="-"|cmd=="*"|cmd=="/")	//加减乘除功能的响应
			{
				
				if(myPanel.op!="")//判断是不是连续操作,如果是,那么立刻将上一步结果显示出来,否则只是将第一次得到的数字获取过来
				{			
					myPanel.num=Double.parseDouble(myPanel.resultField.getText());//获取第二次的操作数,进行运算
					if(myPanel.op=="+") myPanel.result+=myPanel.num;
					if(myPanel.op=="-") myPanel.result-=myPanel.num;
					if(myPanel.op=="*") myPanel.result*=myPanel.num;
					if(myPanel.op=="/") myPanel.result/=myPanel.num;
					if(myPanel.result==(int)(myPanel.result))						//若结果是整数,操作字符串后加“.”显示,否则直接显示
					{
						myPanel.integer=(int)(myPanel.result);
						myPanel.resultField.setText(Integer.toString(myPanel.integer)+".");
					
					}
					else myPanel.resultField.setText(Double.toString(myPanel.result));
				}
				else 
				{
					myPanel.result=Double.parseDouble(myPanel.resultField.getText());
				}
				myPanel.op=cmd;												//恢复计算前原始状态
				myPanel.num=0;
				myPanel.flag=false;
				myPanel.containdot=false;
				myPanel.num=0;
			}
			else if(cmd=="=")				//等于功能响应
			{
				myPanel.num=Double.parseDouble(myPanel.resultField.getText());	//获取第二次的操作数,进行运算
				if(myPanel.op=="+") myPanel.result+=myPanel.num;
				if(myPanel.op=="-") myPanel.result-=myPanel.num;
				if(myPanel.op=="*") myPanel.result*=myPanel.num;
				if(myPanel.op=="/") myPanel.result/=myPanel.num;
				if(myPanel.result==(int)(myPanel.result))							//若结果是整数,操作字符串后加“.”显示,否则直接显示
				{			
					myPanel.integer=(int)(myPanel.result);
					myPanel.resultField.setText(Integer.toString(myPanel.integer)+".");
				}
				else myPanel.resultField.setText(Double.toString(myPanel.result));
				myPanel.num=0;												//恢复计算前原始状态
				myPanel.result=0;
				myPanel.s="";
				myPanel.containdot=false;
				myPanel.op="";
			}
			else if(cmd=="+/-")				//正负功能响应
			{
				myPanel.result=Double.parseDouble(myPanel.resultField.getText());
				myPanel.result=-1*myPanel.result;
				if(myPanel.result==(int)(myPanel.result))							//若结果是整数,操作字符串后加“.”显示,否则直接显示
				{
					myPanel.integer=(int)(myPanel.result);
					myPanel.resultField.setText(Integer.toString(myPanel.integer)+".");
				}
				else myPanel.resultField.setText(Double.toString(myPanel.result));			
			}
			else if(cmd=="sqrt")			//开方功能响应
			{
				myPanel.result=Double.parseDouble(myPanel.resultField.getText());
				myPanel.result=Math.sqrt(myPanel.result);
				if(myPanel.result==(int)(myPanel.result))							//若结果是整数,操作字符串后加“.”显示,否则直接显示
				{
					myPanel.integer=(int)(myPanel.result);
					myPanel.resultField.setText(Integer.toString(myPanel.integer)+".");
				}
				else myPanel.resultField.setText(Double.toString(myPanel.result));
			}
			else if(cmd=="%")				//百分比功能响应
			{
				myPanel.temp=Double.parseDouble(myPanel.resultField.getText());
				if(myPanel.op=="")	myPanel.resultField.setText("0");            //判断是否输入了第二个数,如果没输入只返回0,如果输入了就将第二个数除100后返回屏幕上
				else
					myPanel.temp=Double.parseDouble(myPanel.resultField.getText())/100.0;				
				if(myPanel.temp==(int)(myPanel.temp))								//若结果是整数,操作字符串后加“.”显示,否则直接显示
				{
					myPanel.integer=(int)(myPanel.num);
					myPanel.resultField.setText(Integer.toString(myPanel.integer)+".");
				}
				else myPanel.resultField.setText(Double.toString(myPanel.temp));
			}
			else if(cmd=="1/x")				//倒数功能响应
			{
				myPanel.result=Double.parseDouble(myPanel.resultField.getText());
				myPanel.result=1/myPanel.result;
				if(myPanel.result==(int)(myPanel.result))
				{
					myPanel.integer=(int)(myPanel.result);
					myPanel.resultField.setText(Integer.toString(myPanel.integer)+".");
				}
				else myPanel.resultField.setText(Double.toString(myPanel.result));
			}
			else if(cmd=="C")				//完全清除功能响应
			{
				myPanel.resultField.setText("0.");
				myPanel.num=0;
				myPanel.op="";
				myPanel.s="";
				myPanel.result=0;
				myPanel.temp=0;
				myPanel.flag=true;
				myPanel.containdot=false;	
			}
			else if(cmd=="BackSpace")		//单清除功能响应
			{
				StringBuffer buff=new StringBuffer(myPanel.resultField.getText());	//获取当前操作字符串加“.”形式
				StringBuffer cmdbuff=new StringBuffer(myPanel.s);					//获取当前操作字符串
				if(buff.charAt(buff.length()-1)=='.')							//若最后一位是“.”,即为整数
				{
					if(buff.length()==2)										//若操作字符串长为2,直接置“0.”
					{
						buff.delete(0,buff.length());
						buff.append("0.");
						myPanel.s="";
						myPanel.resultField.setText(buff.toString());
					}
					else														//减一位加“.”输出
					{						
						if(myPanel.containdot)										//若之前数为小数,操作字符串把“.”去掉
						{
							myPanel.containdot=false;
							cmdbuff.deleteCharAt(cmdbuff.length()-1);
							myPanel.s=cmdbuff.toString();
						}
						else 
						{						
							cmdbuff.deleteCharAt(cmdbuff.length()-1);						
							myPanel.s=cmdbuff.toString();
							myPanel.containdot=false;
							myPanel.resultField.setText(cmdbuff.toString()+".");
						}						
					}						
				}
				else															//如果是小数,直接减一位并显示
				{
						
					buff.deleteCharAt(buff.length()-1);
					myPanel.resultField.setText(buff.toString());
					myPanel.s=buff.toString();
				}		
			}		
			else if(cmd=="CE")				//当前内容清除功能响应
			{
				myPanel.resultField.setText("0.");
				myPanel.s="";
				myPanel.containdot=false;
			}
			else if(cmd==".")				//小数点功能响应
			{
				if(myPanel.containdot)
					myPanel.resultField.setText(myPanel.s);
				else 
				{
					if(myPanel.s=="") myPanel.s="0";
					myPanel.resultField.setText(myPanel.s+=".");
					myPanel.containdot=true;
				}
			
			}
			else if(cmd=="MC")				//清除保留功能响应
			{
				myPanel.mem=0;
				myPanel.save.setText("");
			}
			else if(cmd=="MR")				//读取保留功能响应
			{
				if(myPanel.mem==(int)(myPanel.mem))	//若结果是整数,操作字符串后加“.”显示,否则直接显示
				{
					myPanel.integer=(int)(myPanel.mem);
					myPanel.resultField.setText(Integer.toString(myPanel.integer)+".");
				}
				else myPanel.resultField.setText(Double.toString(myPanel.mem));
			}
			else if(cmd=="M+")				//保留相加功能响应
			{
				myPanel.mem+=Double.parseDouble(myPanel.resultField.getText());
				myPanel.save.setText("M");
				myPanel.s="";
			}
			else if(cmd=="MS")				//保留功能响应
			{
				myPanel.mem=Double.parseDouble(myPanel.resultField.getText());
				myPanel.save.setText("M");
				myPanel.s="";
			}
		}
}
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -