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

📄 calculator.java

📁 这是一个计算器的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*题目:设计一个复杂的计算器,能实现Window中计算器的功能,如下图:
⑴定义组件:0~9,+、-、*、/、sqrt()、1/x、%、C=按钮;显示屏文本行
⑵要求能关闭窗口;点击按钮,或键盘数字会在显示屏上输出结果。
⑶具有进制转换功能;*/
package calculator;//把生成的所有class文件保存到一个文件夹里,文件夹就是以后面的那个字符命名
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;//头文件
public class calculator extends Applet 
{
	 boolean first=true;
	 boolean Bn=false;//当前数是否是二进制数的标识符
	 boolean Hn=false;//当前数是否是16进制数的标识符
	 boolean On=false;//当前数是否是8进制数的标识符
	 boolean Dn=true;//当前数是否是十进制数的标识符
	 double number1;//定义第一个按的数字,输入的第一个数字保存在这
	 double number2;//定义第二个按的数字,输入的第二个数字保存在这
	 int yunsuan;//定义加减乘除求余的运算方法
	 double result;//定义结果,结果就保存在这
	 int t=0;//循环用
	boolean IsOper=false;//这个是和当数据框里有数据时,再按下一个数据后,清除原来数据有关的一个东西
	JTextField field=new JTextField(24);//定义最上面那个框,括号里的数字是框的长度,你可以改变看看,就知道用处了
    JButton[] button=new JButton[26];//定义26个按键
    JLabel dec=new JLabel("                                                     ");//提示信息拦
	public void init(){    
        add(field);//增加一个field框
     	field.setHorizontalAlignment(JTextField.RIGHT);
		field.setForeground(Color.black);
		field.setBackground(Color.white);
		field.setText("0");//以上这4个是对field的设置,设置成数字在右边,颜色,初始化状态为0
		this.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));//流式布局的定义,按照左对齐方式,按键之间间隔10
		String anniu[]={"       Backspace        ","           C           ","7","8","9","/","sqrt","4","5","6","*","  %  ","1","2","3",
				"-"," 1/x ","0","±",".","+","   =   ","十进制","二进制","八进制","十六进制"};//把22个按键存放在anniu这个数组里	 
		for(int i=0;i<26;i++){
			button[i]=new JButton(anniu[i]);
			this.add(button[i]);
			button[i].setForeground(Color.blue);	//定义这26个按键,并增加他们,同时给他们都赋上蓝色			
		}  add(dec);
           button[0].setForeground(Color.red);
           button[1].setForeground(Color.red);
           button[5].setForeground(Color.red);
           button[10].setForeground(Color.red); 
           button[15].setForeground(Color.red);
           button[20].setForeground(Color.red);
           button[21].setForeground(Color.red);//以上这几句是吧+,-,*,/,开根号,%,X分之一等号那些按键设置成红色
		   button[22].setForeground(Color.magenta);	
		   button[23].setForeground(Color.magenta);
		   button[24].setForeground(Color.magenta);
		   button[25].setForeground(Color.magenta);//以上的这4个按键是进制按键,单独给它们赋色
	    A a=new A();//数字按钮事件监听
		B b=new B();//清零按键事件监听
		C c=new C();//BS键的事件监听
		D d=new D();//1/x事件监听
		E e=new E();//小数点事件监听
		F f=new F();//运算事件监听
		G g=new G();//运算结果监听
		H h=new H();//正负号监听
        I i=new I();//sqrt的监听
        J j=new J();//2进制转换事件的处理
        K k=new K();//8进制转换事件的处理
        L l=new L();//16进制转换事件的处理
        M m=new M();//10进制转换事件的处理
        //以上是把一些事件,分类,并且把+-*/%都放到了F运算事件监听里
		button[0].addActionListener(c);
		button[1].addActionListener(b);
		button[2].addActionListener(a);
		button[3].addActionListener(a);
		button[4].addActionListener(a);
		button[5].addActionListener(f);
		button[6].addActionListener(i);
		button[7].addActionListener(a);
		button[8].addActionListener(a);
		button[9].addActionListener(a);
		button[10].addActionListener(f);
		button[11].addActionListener(f);
		button[12].addActionListener(a);
		button[13].addActionListener(a);
		button[14].addActionListener(a);
		button[15].addActionListener(f);
		button[16].addActionListener(d);
		button[17].addActionListener(a);
		button[18].addActionListener(h);
		button[19].addActionListener(e);
		button[20].addActionListener(f);
		button[21].addActionListener(g);
		button[22].addActionListener(m);
		button[23].addActionListener(j);
		button[24].addActionListener(k);
		button[25].addActionListener(l);
             //以上的是把每个按键定义到相应的事件里去

		}
		
		class A implements ActionListener{//数字按钮事件监听
		public void actionPerformed(ActionEvent e){
			 String input = e.getActionCommand();//定义input来接收你按的数字并存放
			 if (first) 
	         {	if(input=="0"){//当你一开始按零的时候,系统不应该有响应,应该一直显示一个0
	        	 	 field.setText("");
	        		 first=true;
	         		}
	         	else{
	            field.setText("");
	            first = false;//如果不是按的第一个不是0,那么就相应的输出和保存你按了哪些数字
	         	}
	         }
	         if(IsOper)
	         {
	         	field.setText("");
	         	IsOper=false;
	         }
	         field.setText(field.getText() + input);//以上是判断是否已经按了=号,数据框里是否已经有结果
	                                                //这样在已经有数据的情况下,再按数字会清零
	      }
		}
	class B implements ActionListener{//清零按键事件监听
		public void actionPerformed(ActionEvent e){//如果按了C键也就是清零键,就到这里来处理
			field.setText("0");//按了C键,让文本框显示0,初始为0
			result=0;
			first=true;
			Bn=false;
	        Hn=false;
	        On=false;
	        Dn=true;
	        dec.setText("");
	        button[2].setForeground(Color.blue);
			button[3].setForeground(Color.blue);
			button[4].setForeground(Color.blue);
			button[7].setForeground(Color.blue);
			button[8].setForeground(Color.blue);
			button[9].setForeground(Color.blue);
			button[13].setForeground(Color.blue);
			button[14].setForeground(Color.blue);//以上是把各种参数都初始化以达到清零目的
		}
	}
	class C implements ActionListener{//Bakespace键事件监听
		public void actionPerformed(ActionEvent e){//如果按了Backspace就到这里来处理
			String s1,s2;
			int jflength;//定义个jflength来保存文本框里的数字长度
			s1=field.getText();//把文本框里的东西赋值给S1
			jflength=field.getText().length();//把文本框里的数字的长度赋给jflength
			if(jflength==0||jflength==1)//如果数字长度是1或者0,那么按了Backspace后就直接显示个0
				field.setText("0");
			else{//如果数字长度大于1,则按照下面的方法做
				s2=s1.substring(0,jflength-1);//把文本框里的最后第0号数据一直到jflength-1(也就是倒数第2个数据)传送给S2
				field.setText(s2);//显示S2的内容,那么也就是说已经把最后一个数字去掉了,也就实现了退格的功能
				}
			first=true;
	}
	}
	class D implements ActionListener{//1/x的监听
		public void actionPerformed(ActionEvent e){
			double x=Double.parseDouble(field.getText());//把从文本框输入的数字赋值给X,切X为double型
			if(x==0)
				field.setText("除数不能为零");//如果输入的是0,那么当然就没有倒数了,显示出错信息
			else
				field.setText(""+1/x);//如果输入的不是0,那么直接求X分之一
		}
	}
	class E implements ActionListener{//监听小数点
		public void actionPerformed(ActionEvent e){//这个就是你按下小数点之后的处理
			if(field.getText().trim().indexOf('.')!=-1);//判断数字里是否已经有小数点
		                                                //如果有了再按小数点就不应该有响应
			else if(field.getText().trim().equals("0"))
					field.setText("0"+e.getActionCommand());//如果没有小数点,而第一个数是零
			else                                            //那就直接是0.XXX
				field.setText(field.getText()+e.getActionCommand());
			first=false;//如果前面是数字,那么就直接显示XXX.XXXX
		}
	}
	class F implements ActionListener{//运算监听
		int operation;
		public void actionPerformed(ActionEvent e){//这是对于+ - * / %运算的一个归类
			if(e.getActionCommand()=="+")//如果你点了+运算
				operation=1;//那么跳转到1号处理(下面会有几号处理是怎么处理的)
			else if(e.getActionCommand()=="-")//如果你点的是-运算
				operation=2;//跳转到2号处理
			else if(e.getActionCommand()=="*")//如果你点的是*运算
				operation=3;//跳转到3号处理
			else if(e.getActionCommand()=="/")
				operation=4;
		    else if(e.getActionCommand()=="  %  ")
		        operation=5;
			number1=Double.parseDouble(field.getText());//把输入的第一个数赋值给number1
			switch(operation){
			case 1:
				yunsuan=1;//对于1号运算的定义,也就是+号运算,你对应上面的1号看
				first=true;
				break;
			case 2:
				yunsuan=2;//对于2号运算的定义,也就是-号运算,对应上面的2号看
				first=true;
				break;
			case 3:
				yunsuan=3;
				first=true;
				break;
			case 4:
				yunsuan=4;
				first=true;
				break;
			case 5:
			    yunsuan=5;
			    first=true;
			    break;
			}
			}
	}
		class G implements ActionListener{//运算结果
		public void actionPerformed(ActionEvent e){//也就是对于按了+-,* ,/%号之后再按=号之后应该怎么算,怎么输出结果
			number2=Double.parseDouble(field.getText());
			if(number1>Double.MAX_VALUE&&number1<Double.MIN_VALUE||number2>Double.MAX_VALUE&&number2<Double.MIN_VALUE)
			field.setText("数据超过界限");//这是一个防止数据溢出处理,不过基本用不上,因为数据根本益处不了,就那些运算
			else{
			switch(yunsuan){//如果数据不益处,那么开始处理运算
			case 1://对1号运算,也就是+法怎么运算给出,看看上面定义的1号,联系起来看
				result=number1+number2;//把结果保存在result里
				field.setText(""+result);//在文本框里输出result即输出运算+号后的结果
				break;
			case 2:	//对于2号运算,也就是-法运算应该怎么做,
				result=number1-number2;//用result保存结果
				field.setText(""+result);//输出结果
				break;
			case 3:

⌨️ 快捷键说明

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