简单计算器的实现(awt技术)

package homework;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Calculater extends Frame implements ActionListener
{
    TextField tf=new TextField(30);
    Panel p=new Panel();
    Button b1 = new Button("1");
    Button b2 = new Button("2");
    Button b3 = new Button("3");
    Button b4 = new Button("4");
    Button b5 = new Button("5"); 
    Button b6 = new Button("6");
    Button b7 = new Button("7");
    Button b8 = new Button("8");
    Button b9= new Button("9");
    Button b10 = new Button("0");
    Button b11 = new Button("+"); 
    Button b12 = new Button("-"); 
    Button b13 = new Button("*"); 
    Button b14 = new Button("/"); 
    Button b15 = new Button("="); 
    Button b16 = new Button("C"); 

    private String op = "";
    
    private String str = "";
    
    public String getOp()
    {
        return op;
    }
    
    public void setOp(String op)
    {
        this.op = op;
    }
    
    public String getStr()
    {
        return str;
    }
    
    public void setStr(String str)
    {
        this.str = str;
    }

    public Calculater(String name)
    {
      super(name);
      this.b1.addActionListener(this);
      this.b2.addActionListener(this);
      this.b3.addActionListener(this);
      this.b4.addActionListener(this);
      this.b5.addActionListener(this);
      this.b6.addActionListener(this);
      this.b7.addActionListener(this);
      this.b8.addActionListener(this);
      this.b9.addActionListener(this);
      this.b10.addActionListener(this);
      this.b11.addActionListener(this);
      this.b12.addActionListener(this);
      this.b13.addActionListener(this);
      this.b14.addActionListener(this);
      this.b15.addActionListener(this);
      this.b16.addActionListener(this);
      this.setLayout(new BorderLayout());
      this.add(tf,BorderLayout.NORTH);
      this.add(p,BorderLayout.CENTER);
      this.p.setLayout(new GridLayout(4,4));
      this.p.add(b7);
      this.p.add(b8);
      this.p.add(b9);
      this.p.add(b13);
      this.p.add(b4);
      this.p.add(b5);
      this.p.add(b6);
      this.p.add(b12);
      this.p.add(b1);
      this.p.add(b2);
      this.p.add(b3);
      this.p.add(b11);
      this.p.add(b10); 
      this.p.add(b16);
      this.p.add(b15);
      this.p.add(b14);
      this.setSize(400, 300);
      this.addWindowListener(new WindowAdapter()
      {
          public void windowClosing(WindowEvent e)
          {
              System.exit(0);
          }
      });
      this.setVisible(true);
    }
    

    
    public static void main(String[] args)
    {
        new Calculater("Calculater").setResizable(false);
    }

 

    @Override
    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        if("0".equals(command))
        {
          this.setStr(str+="0");
          this.tf.setText(str);

        } 
        
        if("1".equals(command))
        {
            this.setStr(str+="1");
            this.tf.setText(str);

        }
        if("2".equals(command))
        {  
            this.setStr(str+="2");
        this.tf.setText(str);
        }
        
        if("3".equals(command))
        {
            this.setStr(str+="3");
            this.tf.setText(str);

        }
        
        if("4".equals(command))
        {
            this.setStr(str+="4");
            this.tf.setText(str);

        }
        
        if("5".equals(command))
        {
            this.setStr(str+="5");
            this.tf.setText(str);

        }
        
        if("6".equals(command))
        {
            this.setStr(str+="6");
            this.tf.setText(str);

        }
        
        if("7".equals(command))
        {
            this.setStr(str+="7");
            this.tf.setText(str);

        }
        
        if("8".equals(command))
        {
            this.setStr(str+="8");
            this.tf.setText(str);

        }
        
        if("9".equals(command))
        {
            this.setStr(str+="9");
            this.tf.setText(str);

        }
        
        
        if("+".equals(command))
        {
            this.setStr(str+="+");
            this.setOp("+");
            this.tf.setText(str);
        }
        
        if("-".equals(command))
        {
            this.setStr(str+="-");
            this.setOp("-");
            this.tf.setText(str);

        }
        
        if("*".equals(command))
        {
            this.setStr(str+="*");
            this.setOp("*");
            this.tf.setText(str);
        }
        
        if("/".equals(command))
        {
            this.setStr(str+="/");
            this.setOp("/");
            this.tf.setText(str);
        }
        
        if("=".equals(command))
        {
           String result[]=this.getStr().split("\\"+this.getOp());
           int a=Integer.parseInt(result[0]);
           int b=Integer.parseInt(result[1]);
           int d=0;
     
           if(op.equals("+"))
           {
               d=a+b;
               this.tf.setText(new Integer(d).toString());
               
           }
           else if(op.equals("-"))
           {
               d=a-b;
               this.tf.setText(new Integer(d).toString());
           }
           else if(op.equals("*"))
           {
               d=a*b;
               this.tf.setText(new Integer(d).toString());
           }
           else  if(op.equals("/"))
           {
     
               double dd=(double)a/(double)b;
               this.tf.setText(dd+"");
           }

        }
        
        if("C".equals(command))
        {
           this.tf.setText("");
           
           this.setStr("");
           this.setOp("");
        }
        
        
    }
}


 


 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值