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

📄 calculator.java

📁 这是一个计算器的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				result=number1*number2;//*运算
				field.setText(""+result);
				break;
			case 4:
				if(number2==0)//除运算,但要判断除数,也就是第二个输入的数是不是零
					field.setText("除数不能为零");//零当然不能为除数,如果你按了零,那么输出出错信息
				else{
				result=number1/number2;//正常运算除法
				field.setText(""+result);
				}
				break;
			case 5:
			    result=number1%number2;//求余运算,结果保存在result中
			    field.setText(""+result);//输出结果
			    break;
			}
			}
			IsOper=true;//这个是和当文本框中已经有了一次运算结果后,再按一个数字时,应该把原来的数字清除,直接显示当前
			//输入的数字,而不是在原来的结果后面加上你按的数字,有关的一句代码
			}
	}
	class H implements ActionListener{//正负号监听
		public void actionPerformed(ActionEvent e){
			double s=Double.parseDouble(field.getText());			
			s=s*(-1);
			field.setText(""+s);
		}
	}
		class I implements ActionListener{//sqrt的监听
		public void actionPerformed(ActionEvent e){//求根号的运算
				double x=Double.parseDouble(field.getText());//把输入的数字存放在X中并定义为double型
					if(x<=0)//如果输入的数字小于零,那么不能开根号
				field.setText("负数不能开平方");//输出错误提示
			else
				field.setText(""+Math.sqrt(x));//如果大于零,那么就调用Math。sqrt这个函数直接求根号
			
			}}
		class J implements ActionListener{//按二进制按钮后的事件处理
			public void actionPerformed(ActionEvent e){
				button[2].setForeground(Color.green);
				button[3].setForeground(Color.green);
				button[4].setForeground(Color.green);
				button[7].setForeground(Color.green);
				button[8].setForeground(Color.green);
				button[9].setForeground(Color.green);
				button[13].setForeground(Color.green);
				button[14].setForeground(Color.green);
				dec.setText("只可以按0-1按钮");//以上是把除0.1之外的按钮变色,并且输出提示信息
			if(Dn)//如果当前数是十进制数
				{  
				double x=Double.parseDouble(field.getText());
			    int s=(int)x;//强制转换为整型
				String show=Integer.toBinaryString(s);//直接调用函数转化为二进制
				field.setText(""+show);	
				Dn=false;
				Bn=true;}		
			else if(Bn==true)//如果当前数是二进制数
				{double x=Double.parseDouble(field.getText());
			     int s=(int)x;//强制转换为整型		
				 field.setText(""+s);}
			else if(On==true){//如果当前数是八进制数
			    String s1=field.getText();
			    int x1=Integer.parseInt(s1,8);//先把八进制转换为十进制
			    String show=Integer.toBinaryString(x1);//再把十进制转换为二进制
			    field.setText(""+show);
			    On=false;
			    Bn=true;
			}	
			else if(Hn==true){//如果当前数是十六进制数
				String s1=field.getText();
				int x1=Integer.parseInt(s1,16);//先把十六进制数转换为十进制数
				String show=Integer.toBinaryString(x1);//再把十进制数转换为二进制数
				field.setText(""+show);
				Hn=false;
				Bn=true;
			}	
			}
		}   
		class K implements ActionListener{//按八进制按钮后的事件处理
			public void actionPerformed(ActionEvent e){
            button[2].setForeground(Color.blue);
			button[3].setForeground(Color.green);
			button[4].setForeground(Color.green);
			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);
			dec.setText("只可以按0-7按钮");//对于八进制,8和9不能用,改变它们颜色,并给出提示信息
			if(Dn)//如果当前数是十进制数
				{double x=Double.parseDouble(field.getText());
			    int s=(int)x;//强制转换为整型
				String show=Integer.toOctalString(s);//直接调用函数转化为八进制数
				field.setText(""+show);	
				Dn=false;
				On=true;}		
			else if(On==true)//如果当前数是八进制数
				{   double x=Double.parseDouble(field.getText());
			        int s=(int)x;//强制转换为整型
					field.setText(""+s);}//直接输出
			else if(Bn==true){//如果当前数是二进制数
			    String s1=field.getText();
			    int x1=Integer.parseInt(s1,2);//先把二进制转化为十进制数
			    String show=Integer.toOctalString(x1);//再转化为八进制数
			    field.setText(""+show);
			    Bn=false;
			    On=true;
			}	
			else if(Hn==true){//如果当前数是十六进制数
				String s1=field.getText();
				int x1=Integer.parseInt(s1,16);//先把十六进制数转换为十进制数
				String show=Integer.toOctalString(x1);//再调用函数转换为八进制数
				field.setText(""+show);
				Hn=false;
				On=true;
			}				
			}
		}
		class L implements ActionListener{//按十六进制按钮后的事件处理
			public void actionPerformed(ActionEvent e){
				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);
				dec.setText("");//以上是把按键恢复原有颜色,提示信息不输出
			if(Dn)//如果当前数是十进制数
				{double x=Double.parseDouble(field.getText());
			    int s=(int)x;//强制转换为整型
				String show=Integer.toHexString(s);//直接调用函数转换为十六进制
				field.setText(""+show);	
				Dn=false;
				Hn=true;}		
			else if(Hn==true)//如果当前数是十六进制
				{	double x=Double.parseDouble(field.getText());
			        int s=(int)x;//强制转换为整型
					field.setText(""+s);}//直接输出
			else if(On==true){//如果当前数是八进制数
			    String s1=field.getText();
			    int x1=Integer.parseInt(s1,8);//先把八进制数转换为十进制数
			    String show=Integer.toHexString(x1);//再调用函数转换为十六进制数
			    field.setText(""+show);
			    On=false;
			    Hn=true;
			}	
			else if(Bn==true){//如果当前数是二进制数
				String s1=field.getText();
				int x1=Integer.parseInt(s1,2);//先把它转换为十进制数
				String show=Integer.toHexString(x1);//再把它转换为十六进制数
				field.setText(""+show);
				Bn=false;
				Hn=true;
			}			
			}
		}
		class M implements ActionListener{//按十进制按钮后的事件处理
			public void actionPerformed(ActionEvent e){
				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);
				dec.setText("");//按键颜色的恢复
			if(Dn)//如果当前数是十进制
				{	double x=Double.parseDouble(field.getText());
			        int s=(int)x;//强制转换为整型
					field.setText(""+s);}//直接输出		
			else if(Hn==true)//如果当前数是十六进制数
				{					
				String s1=field.getText();
			    int x1=Integer.parseInt(s1,16);//转换为十进制数并输出
			    field.setText(""+x1);
			    Hn=false;
			    Dn=true;}
			else if(On==true){//如果当前数是八进制数
			    String s1=field.getText();
			    int x1=Integer.parseInt(s1,8);//转换为十进制数并输出
			    field.setText(""+x1);
			    On=false;
			    Dn=true;
			}	
			else if(Bn==true){//如果当前数是二进制数
				String s1=field.getText();
				int x1=Integer.parseInt(s1,2);//转换为十进制数并输出
				field.setText(""+x1);
				Bn=false;
				Dn=true;
			}			
			}
		}
			
		public static void main(String args[]){//主函数
		Frame x=new Frame("计算器  Made by SYL");//定义最上面框里的字,并且引号里的就是那里显示的,你可以自己设置
		calculator y=new calculator();
		x.add(y);
		y.init();//以上都是按照书上格式抄的,书上那样我也那样,也就是一些必要的声明,加定义吧
		x.addWindowListener(new closeW());//关闭窗口功能的定义
		x.addComponentListener(new resizeW());//锁定窗口功能的定义
		x.setSize(290,340);//设置Applet程序的大小,不要更改,否则按键布局会乱
		x.setVisible(true);
		
	}
}
	class closeW extends WindowAdapter//关闭窗口功能
	{
	public void windowClosing(WindowEvent e)
		{
			System.exit(0);			
		}
	}
	class resizeW extends ComponentAdapter//缩定窗口,防止拉伸出现布局混乱
	{
    public void componentResized(ComponentEvent e)
    {
    	e.getComponent().setSize(290,340);
    }		
	}

⌨️ 快捷键说明

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