计算器源代码
一、计算器源代码文件名:computer1.java
import java.awt.*;
import java.awt.event.*;
public class computer1 extends Frame implements ActionListener{
                                //声明窗口类并实现动作事件接口。
Button n0,n1,n2,n3,n4,n5,n6,n7,n8,n9;//声明数字按钮
Button op,os,om,od,oe,oc;//声明操作按钮
        TextField tfd;//声明文本框
        String flg,rslt;//声明标志串、结果串
        Panel p1,p2,p3;//声明面板
        int i1,i2;
        float flt;
    computer1(){
        super("加减乘除计算器");
            n0 = new Button("0");//实现各按钮
            n1 = new Button("1");
            n2 = new Button("2");
            n3 = new Button("3");
            n4 = new Button("4");
            n5 = new Button("5");
            n6 = new Button("6");
            n7 = new Button("7");
            n8 = new Button("8");
            n9 = new Button("9");
            op = new Button("加");
            os = new Button("减");
            om = new Button("乘");
            od = new Button("除");
            oe = new Button("=");
            oc = new Button("c");
            tfd = new TextField(20);//实现文本框
            p1=new Panel();//实现各面板
            p2=new Panel();
            p3=new Panel();
            setLayout(new FlowLayout());//布局设计,用于安排按钮位置
        p1.add(n0);//将各数字按钮放入p1中
        p1.add(n1);
        p1.add(n2);
        p1.add(n3);
        p1.add(n4);
        p1.add(n5);
        p1.add(n6);
        p1.add(n7);
        p1.add(n8);
        p1.add(n9);
        p2.add(op);//将各操作按钮放入p2、p3中
        p2.add(os);
        p2.add(om);
        p2.add(od);
        p3.add(oe);
        p3.add(oc);
        setLayout(new BorderLayout());//布局设计,用于安排面板位置
            add("North",tfd);
            add("West",p1);
            add("Center",p2);
            add("East",p3);
        n0.addActionListener(this);//注册到各按钮
        n1.addActionListener(this);
        n2.addActionListener(this);
        n3.addActionListener(this);
        n4.addActionListener(this);
        n5.addActionListener(this);
        n6.addActionListener(this);
        n7.addActionListener(this);
        n8.addActionListener(this);
        n9.addActionListener(this);
        op.addActionListener(this);
        os.addActionListener(this);
        om.addActionListener(this);
        od.addActionListener(this);
        oe.addActionListener(this);
        oc.addActionListener(this);
        addWindowListener(new closeWin());   
        setSize(600,100);//确定窗口的尺寸
        setVisible(true);
    }
    public static void main (String args[]){
        new computer1();
    }
    public void actionPerformed(ActionEvent e){//处理鼠标事件的方法
        try{//异常处理   
        Source()==n0)//按数字键时
                tfd.Text()+"0");
        Source()==n1)
                tfd.Text()+"1");
        Source()==n2)
                tfd.Text()+"2");
        Source()==n3)
                tfd.Text()+"3");   
        Source()==n4)
                tfd.Text()+"4");
        Source()==n5)
                tfd.Text()+"5");   
        Source()==n6)
                tfd.Text()+"6");
        Source()==n7)
                tfd.Text()+"7");
        Source()==n8)
                tfd.Text()+"8");
        Source()==n9)
                tfd.Text()+"9");   
        Source()==op){//按加号键时
                i1 = Integer.Text());   
            tfd.setText("");
                flg = "op";
            }
        Source()==os){//按减号键时
                i1 = Integer.Text());       
                tfd.setText("");
                flg = "os";vb计算器代码大全
        }
            Source()==om){//按乘号键时
                i1 = Integer.Text());   
                tfd.setText("");
                flg = "om";
        }
            Source()==od){//按除号键时
                i1 = Integer.Text());   
                tfd.setText("");
            flg = "od";
            }
            Source()==oe){//按等号键时
            i2 = Integer.Text());   
                if(flg=="op"){
                String(i1+i2);
                }
                if(flg=="os"){
                    String(i1-i2);
            }
                if(flg=="om"){
                    String(i1*i2);
                }
            if(flg=="od"){//除法需做小数处理
                    flt=((float)i1)/((float)i2);
                    String(flt);
                }
                tfd.setText(rslt);
        }
            Source()==oc){//按清除键时
                tfd.setText("");   
                flg = "";
        }
        }catch(Exception ex){}//扑捉到异常,但不进行处理
}
}
class closeWin extends WindowAdapter{        //关闭窗口
        public void windowClosing(WindowEvent e){
            Frame frm=(Frame)(e.getSource());
        frm.dispose();
            it(0);
    }
}
二、计算器界面
三、修改后计算器界面

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。