JAVA实现简易计算器

目前的JAVA计算器只能进行一步加减乘除法,点号也不能用,还在改进中import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame mainframe;
Container con;
JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,
btn0,btnAdd,btnSub,btnMul,btnDiv,btnEq,btnCln,btnDot;
JTextField texSum;
static int num1,num2;
static double result;
static String operator;
static boolean first=true;
static String temp;
public Calculator(){
mainframe=new JFrame("计算器");
con=mainframe.getContentPane();
con.setLayout(new FlowLayout());
texSum=new JTextField();   //新建输入和结果框
texSum.setColumns(10);     //控制输入框的宽度
con.add(texSum);           //添加到中间容器中

btnEq=new JButton("=");          //新建等于按钮
btnEq.addActionListener(this);  //添加监听事件
con.add(btnEq);                 //添加到中间容器中
con.add(Box.createHorizontalStrut(30000));

btn1=new JButton("1");          //新建1按钮
btn1.addActionListener(this);  //添加监听事件
con.add(btn1);                 //添加到中间容器中

btn2=new JButton("2");          //新建2按钮
btn2.addActionListener(this);  //添加监听事件
con.add(btn2);                 //添加到中间容器中

btn3=new JButton("3");          //新建3按钮
btn3.addActionListener(this);  //添加监听事件
con.add(btn3);                 //添加到中间容器中

btnAdd=new JButton("+");          //新建+按钮
btnAdd.addActionListener(this);  //添加监听事件
con.add(btnAdd);                 //添加到中间容器中
con.add(Box.createHorizontalStrut(30000));

btn4=new JButton("4");          //新建4按钮
btn4.addActionListener(this);  //添加监听事件
con.add(btn4);                 //添加到中间容器中

btn5=new JButton("5");          //新建5按钮
btn5.addActionListener(this);  //添加监听事件
con.add(btn5);                 //添加到中间容器中

btn6=new JButton("6");          //新建6按钮
btn6.addActionListener(this);  //添加监听事件
con.add(btn6);                 //添加到中间容器中

btnSub=new JButton("-");          //新建-按钮
btnSub.addActionListener(this);  //添加监听事件
con.add(btnSub);                 //添加到中间容器中
con.add(Box.createHorizontalStrut(30000));

btn7=new JButton("7");          //新建7按钮
btn7.addActionListener(this);  //添加监听事件
con.add(btn7);                 //添加到中间容器中

btn8=new JButton("8");          //新建8按钮
btn8.addActionListener(this);  //添加监听事件
con.add(btn8);                 //添加到中间容器中

btn9=new JButton("9");          //新建9按钮
btn9.addActionListener(this);  //添加监听事件
con.add(btn9);                 //添加到中间容器中

btnMul=new JButton("*");          //新建乘按钮
btnMul.addActionListener(this);  //添加监听事件
con.add(btnMul); 
con.add(Box.createHorizontalStrut(30000));//添加到中间容器中

btn0=new JButton("0");          //新建0按钮
btn0.addActionListener(this);  //添加监听事件
con.add(btn0);                 //添加到中间容器中

btnDot=new JButton(".");          //新建点按钮
btnDot.addActionListener(this);  //添加监听事件
con.add(btnDot);                 //添加到中间容器中

btnCln=new JButton("C");          //新建清除按钮
btnCln.addActionListener(this);  //添加监听事件
con.add(btnCln);                 //添加到中间容器中

btnDiv=new JButton("/");          //新建除法按钮
btnDiv.addActionListener(this);  //添加监听事件
con.add(btnDiv);                 //添加到中间容器中
con.add(Box.createHorizontalStrut(30000));
mainframe.setSize(300,300);
mainframe.setVisible(true);
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void num(Object obj){   //用来将按下去的单个数字组成一个数
 
if(obj==btn0||obj==btn1||obj==btn2||obj==btn3|| obj==btn4||obj==btn5||obj==btn6||obj==btn7||obj==btn8||obj==btn9){
        if (first) //如果是第一个操作数的第1个数字
             {
        texSum.setText(null); //清除文本框
                 first = false;  //改变状态,已经清空文本框了
             }
        temp=texSum.getText();
        if(obj==btn0){
        temp+='0';
        }
        if(obj==btn1){
        temp+='1';
        }
        if(obj==btn2){
        temp+='2';
        }
        if(obj==btn3){
        temp+='3';
        }
        if(obj==btn4){
        temp+='4';
        }
        if(obj==btn5){
        temp+='5';
        }
        if(obj==btn6){
        temp+='6';
        }
        if(obj==btn7){
        temp+='7';
        }
        if(obj==btn8){
        temp+='8';
        }
        if(obj==btn9){
        temp+='9';
        }
       
        if (temp.substring(0, 1)=="0"&&temp.length()>=1) { //当输入的数字位数大于1,并且第一个数字是0时,去掉开头的0
         
        temp=temp.substring(1);//去掉开头的O
             }
         
         } //第一个if结束   
texSum.setText(temp);

}
   
public void actionPerformed(ActionEvent e){
   
    Object obj=e.getSource();
    if(obj==btn0||obj==btn1||obj==btn2||obj==btn3|| obj==btn4||obj==btn5||obj==btn6||obj==btn7||obj==btn8||obj==btn9){
    num(obj);   //调用num方法来组成数字
    }
   
    if(obj==btnAdd||obj==btnSub||obj==btnMul||obj==btnDiv){  //保存第一个数且记录输入的符号
    // + - * / 按钮的单击事件
       num1 =Integer.parseInt(texSum.getText());   //保存第一个操作数
          if(obj==btnAdd){
          operator="btnAdd";  
          }
          if(obj==btnSub){
          operator="btnSub";  
          }
          if(obj==btnMul){
          operator="btnMul";  
          }
          if(obj==btnDiv){
          operator="btnDiv";  
          }
          texSum.setText(null);
        
    }
    if(obj==btnEq){     //=号按钮的单击事件
            num2 = Integer.parseInt(texSum.getText());  //保存第二个操作数
            if(operator=="btnAdd"){
            result = num1 + num2;
            }
            if(operator=="btnSub"){
            result = num1 - num2;
            }
            if(operator=="btnMul"){
            result = num1 * num2;
            }
            if(operator=="btnDiv"){
            result = num1 / num2;
            }
            
           String a=Double.toString(result);
            texSum.setText(a);  //显示运算结果
            first = true; //恢复初始值
   
    }
    if(obj==btnCln){
    texSum.setText(null);
    }
     
    }
    public static void main(String args[]){
    new Calculator();
    }
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
	JFrame mainframe;
	Container con;
	JButton btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,
	btn0,btnAdd,btnSub,btnMul,btnDiv,btnEq,btnCln,btnDot;
	JTextField texSum;
	static int num1,num2;
	static double result;
	static String operator;
	static boolean first=true;
	static String temp;
	public Calculator(){
		mainframe=new JFrame("计算器");
		con=mainframe.getContentPane();
		con.setLayout(new FlowLayout());
		texSum=new JTextField();   //新建输入和结果框
		texSum.setColumns(10);     //控制输入框的宽度
		con.add(texSum);           //添加到中间容器中
		
		btnEq=new JButton("=");          //新建等于按钮
		btnEq.addActionListener(this);  //添加监听事件
		con.add(btnEq);                 //添加到中间容器中
		con.add(Box.createHorizontalStrut(30000));
		
		btn1=new JButton("1");          //新建1按钮
		btn1.addActionListener(this);  //添加监听事件
		con.add(btn1);                 //添加到中间容器中
		
		btn2=new JButton("2");          //新建2按钮
		btn2.addActionListener(this);  //添加监听事件
		con.add(btn2);                 //添加到中间容器中
		
		btn3=new JButton("3");          //新建3按钮
		btn3.addActionListener(this);  //添加监听事件
		con.add(btn3);                 //添加到中间容器中
		
		btnAdd=new JButton("+");          //新建+按钮
		btnAdd.addActionListener(this);  //添加监听事件
		con.add(btnAdd);                 //添加到中间容器中
		con.add(Box.createHorizontalStrut(30000));
		
		btn4=new JButton("4");          //新建4按钮
		btn4.addActionListener(this);  //添加监听事件
		con.add(btn4);                 //添加到中间容器中
		
		btn5=new JButton("5");          //新建5按钮
		btn5.addActionListener(this);  //添加监听事件
		con.add(btn5);                 //添加到中间容器中
		
		btn6=new JButton("6");          //新建6按钮
		btn6.addActionListener(this);  //添加监听事件
		con.add(btn6);                 //添加到中间容器中
		
		btnSub=new JButton("-");          //新建-按钮
		btnSub.addActionListener(this);  //添加监听事件
		con.add(btnSub);                 //添加到中间容器中
		con.add(Box.createHorizontalStrut(30000));
		
		btn7=new JButton("7");          //新建7按钮
		btn7.addActionListener(this);  //添加监听事件
		con.add(btn7);                 //添加到中间容器中
		
		btn8=new JButton("8");          //新建8按钮
		btn8.addActionListener(this);  //添加监听事件
		con.add(btn8);                 //添加到中间容器中
		
		btn9=new JButton("9");          //新建9按钮
		btn9.addActionListener(this);  //添加监听事件
		con.add(btn9);                 //添加到中间容器中
		
		btnMul=new JButton("*");          //新建乘按钮
		btnMul.addActionListener(this);  //添加监听事件
		con.add(btnMul); 
		con.add(Box.createHorizontalStrut(30000));//添加到中间容器中
		
		btn0=new JButton("0");          //新建0按钮
		btn0.addActionListener(this);  //添加监听事件
		con.add(btn0);                 //添加到中间容器中
		
		btnDot=new JButton(".");          //新建点按钮
		btnDot.addActionListener(this);  //添加监听事件
		con.add(btnDot);                 //添加到中间容器中
		
		btnCln=new JButton("C");          //新建清除按钮
		btnCln.addActionListener(this);  //添加监听事件
		con.add(btnCln);                 //添加到中间容器中
		
		btnDiv=new JButton("/");          //新建除法按钮
		btnDiv.addActionListener(this);  //添加监听事件
		con.add(btnDiv);                 //添加到中间容器中
		con.add(Box.createHorizontalStrut(30000));
		mainframe.setSize(300,300);
		mainframe.setVisible(true);
		mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public void num(Object obj){   //用来将按下去的单个数字组成一个数
		 
		 if(obj==btn0||obj==btn1||obj==btn2||obj==btn3|| obj==btn4||obj==btn5||obj==btn6||obj==btn7||obj==btn8||obj==btn9){
        	 if (first) //如果是第一个操作数的第1个数字
             {
        		 texSum.setText(null); //清除文本框
                 first = false;  //改变状态,已经清空文本框了
             }
        	 temp=texSum.getText();
        	 if(obj==btn0){
        		 temp+='0';
        	 }
        	 if(obj==btn1){
        		 temp+='1';
        	 }
        	 if(obj==btn2){
        		 temp+='2';
        	 }
        	 if(obj==btn3){
        		 temp+='3';
        	 }
        	 if(obj==btn4){
        		 temp+='4';
        	 }
        	 if(obj==btn5){
        		 temp+='5';
        	 }
        	 if(obj==btn6){
        		 temp+='6';
        	 }
        	 if(obj==btn7){
        		 temp+='7';
        	 }
        	 if(obj==btn8){
        		 temp+='8';
        	 }
        	 if(obj==btn9){
        		 temp+='9';
        	 }
        	
        	 if (temp.substring(0, 1)=="0"&&temp.length()>=1) { //当输入的数字位数大于1,并且第一个数字是0时,去掉开头的0
        		 
        		 temp=temp.substring(1);//去掉开头的O
             }
        	 
         } //第一个if结束	   
		 texSum.setText(temp);
		
	}
   
	public void actionPerformed(ActionEvent e){
    	
    	Object obj=e.getSource();
    	 if(obj==btn0||obj==btn1||obj==btn2||obj==btn3|| obj==btn4||obj==btn5||obj==btn6||obj==btn7||obj==btn8||obj==btn9){
    	 num(obj);   //调用num方法来组成数字
    	 }
    	
    	 if(obj==btnAdd||obj==btnSub||obj==btnMul||obj==btnDiv){  //保存第一个数且记录输入的符号
    		// + - * / 按钮的单击事件
    		    num1 =Integer.parseInt(texSum.getText());   //保存第一个操作数
    	       if(obj==btnAdd){
    	    	   operator="btnAdd";  
    	       }
    	       if(obj==btnSub){
    	    	   operator="btnSub";  
    	       }
    	       if(obj==btnMul){
    	    	   operator="btnMul";  
    	       }
    	       if(obj==btnDiv){
    	    	   operator="btnDiv";  
    	       }
    	       texSum.setText(null);
    	     
    	 }
    	if(obj==btnEq){     //=号按钮的单击事件
            num2 = Integer.parseInt(texSum.getText());  //保存第二个操作数
            if(operator=="btnAdd"){
            	result = num1 + num2;
            }
            if(operator=="btnSub"){
            	result = num1 - num2;
            }
            if(operator=="btnMul"){
            	result = num1 * num2;
            }
            if(operator=="btnDiv"){
            	 result = num1 / num2;
            }
            
           String a=Double.toString(result);
            texSum.setText(a);  //显示运算结果
            first = true; //恢复初始值
    		
    	}
    	if(obj==btnCln){
    		texSum.setText(null);
    	}
    	 
    }
    public static void main(String args[]){
    	new Calculator();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值