当前位置导航:炫浪网>>网络学院>>网页制作>>JSP教程

用了12个小时完成一个计算器小作业


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

import javax.swing.JTextField;import java.awt.Color;

public class createFrame{    Frame f;    JTextField text,text2;    Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,        b18,b19,b20,b21,b22,b23,b24,b25,b26,b27;    double op1,op2,op2x,op3;//运算的三个数:op1,op2是运算数,op3是运算结果    char op=0,opx=0,opp=0;//运算操作符    boolean b=false;//我按过0键了,下次该按.了    boolean c=false;//按过运算符了,下次按数字时该清屏显示    boolean firstdeng=true;//第一次做=

    double mjia=0;

    public static void main(String[] args)    {        createFrame cf=new createFrame();        cf.createWindow();    }        public void createWindow()    {        f = new Frame("calculator");        f.setBackground(Color.LIGHT_GRAY);                // 增加关闭事件的监听器        this.shutwindow(f);

        text=new JTextField();        text.setHorizontalAlignment(JTextField.RIGHT);        text.setSize(250,22);        text.setLocation(5,25);        text.setEditable(false);        text.setBackground(Color.WHITE);        text.setText("0.");                text2=new JTextField();        text2.setHorizontalAlignment(JTextField.CENTER);        text2.setSize(37,22);        text2.setLocation(5,50);        text2.setEditable(false);        text2.setText("");                text2.setBackground(Color.LIGHT_GRAY);                b1 = new Button("BackSpace");         b1.setSize(70,22);        b1.setLocation(52,50);                b2 = new Button("CE");         b2.setSize(60,22);        b2.setLocation(128,50);                b3 = new Button("C");         b3.setSize(60,22);        b3.setLocation(194,50);                        b4 = new Button("MC");        b4.setSize(37,22);        b4.setLocation(5,75);        b5 = new Button("7");        b5.setSize(37,22);        b5.setLocation(50,75);        b6 = new Button("8");        b6.setSize(37,22);        b6.setLocation(93,75);        b7 = new Button("9");        b7.setSize(37,22);        b7.setLocation(136,75);        b8 = new Button("/");        b8.setSize(37,22);        b8.setLocation(179,75);        b9 = new Button("sqrt");        b9.setSize(37,22);        b9.setLocation(218,75);                b10 = new Button("MR");        b10.setSize(37,22);        b10.setLocation(5,100);        b11 = new Button("4");        b11.setSize(37,22);        b11.setLocation(50,100);        b12 = new Button("5");        b12.setSize(37,22);        b12.setLocation(93,100);        b13 = new Button("6");        b13.setSize(37,22);        b13.setLocation(136,100);        b14 = new Button("*");        b14.setSize(37,22);        b14.setLocation(179,100);        b15 = new Button("%");        b15.setSize(37,22);        b15.setLocation(218,100);                b16 = new Button("MS");        b16.setSize(37,22);        b16.setLocation(5,125);        b17 = new Button("1");        b17.setSize(37,22);        b17.setLocation(50,125);        b18 = new Button("2");        b18.setSize(37,22);        b18.setLocation(93,125);        b19 = new Button("3");        b19.setSize(37,22);        b19.setLocation(136,125);        b20 = new Button("-");        b20.setSize(37,22);        b20.setLocation(179,125);        b21 = new Button("1/x");        b21.setSize(37,22);        b21.setLocation(218,125);                b22 = new Button("M+");        b22.setSize(37,22);        b22.setLocation(5,150);        b23 = new Button("0");        b23.setSize(37,22);        b23.setLocation(50,150);        b24 = new Button("+/-");        b24.setSize(37,22);        b24.setLocation(93,150);        b25 = new Button(".");        b25.setSize(37,22);        b25.setLocation(136,150);        b26 = new Button("+");        b26.setSize(37,22);        b26.setLocation(179,150);        b27 = new Button("=");        b27.setSize(37,22);        b27.setLocation(218,150);                f.setLayout(null);       

        f.add(text);        f.add(text2);                b1.addActionListener(new ButtonHandler());        b2.addActionListener(new ButtonHandler());        b3.addActionListener(new ButtonHandler());        b4.addActionListener(new ButtonHandler());        b5.addActionListener(new ButtonHandler());        b6.addActionListener(new ButtonHandler());        b7.addActionListener(new ButtonHandler());        b8.addActionListener(new ButtonHandler());        b9.addActionListener(new ButtonHandler());        b10.addActionListener(new ButtonHandler());        b11.addActionListener(new ButtonHandler());        b12.addActionListener(new ButtonHandler());        b13.addActionListener(new ButtonHandler());        b14.addActionListener(new ButtonHandler());        b15.addActionListener(new ButtonHandler());        b16.addActionListener(new ButtonHandler());        b17.addActionListener(new ButtonHandler());        b18.addActionListener(new ButtonHandler());        b19.addActionListener(new ButtonHandler());        b20.addActionListener(new ButtonHandler());        b21.addActionListener(new ButtonHandler());        b22.addActionListener(new ButtonHandler());        b23.addActionListener(new ButtonHandler());        b24.addActionListener(new ButtonHandler());        b25.addActionListener(new ButtonHandler());        b26.addActionListener(new ButtonHandler());        b27.addActionListener(new ButtonHandler());

                f.add(b1);        f.add(b2);        f.add(b3);                f.add(b4);        f.add(b5);        f.add(b6);        f.add(b7);        f.add(b8);        f.add(b9);                f.add(b10);        f.add(b11);        f.add(b12);        f.add(b13);        f.add(b14);        f.add(b15);                f.add(b16);        f.add(b17);        f.add(b18);        f.add(b19);        f.add(b20);        f.add(b21);                f.add(b22);        f.add(b23);        f.add(b24);        f.add(b25);        f.add(b26);        f.add(b27);                    f.setResizable(false);        f.setSize(260,180);                //窗口居中显示               this.centershow(260,180,f);                        f.setVisible(true);            }        public void centershow(int x,int y,Frame f)    {        //窗口居中显示        Toolkit tk=Toolkit.getDefaultToolkit();        Dimension d=tk.getScreenSize();        int screenHeight=d.height;        int screenWidth=d.width;        f.setLocation((screenWidth-x)/2,(screenHeight-y)/2);    }        public void shutwindow(Frame f)    {        //Add a listener for the close event        f.addWindowListener(new WindowAdapter(){            public void windowClosing(WindowEvent evt)             {                            // 退出程序                System.exit(0);            }        });    }        public double getCurrentNum()//得到当前文本框内数字    {                return 1;    }        //四则运算    public double calculateNow(double op1,char op,double op2)    {        double op1b=0.0;        switch(op)        {            case '+':                op1b=op1+op2;             break;         case '-':             op1b=op1-op2;          break;         case '*':             op1b=op1*op2;             break;         case '/':             if(op2==0.0)                 op1b=op1;             else                 op1b=op1/op2;             break;         case '%':             if(op2==0.0)                 op1b=op1;             else                 op1b=op1%op2;             break;         case 0:             op1b=op2;             break;        }        return op1b;    }         class ButtonHandler implements ActionListener    {     public void actionPerformed(ActionEvent e)        {      System.out.println("Button's lable is :" + e.getActionCommand()+"||"              +createFrame.this.op1              +createFrame.this.op              +createFrame.this.op2              +"="              +createFrame.this.op3);               String command=e.getActionCommand();         String currentText=text.getText();         if(currentText.equals("函数输入无效.") || currentText.equals("除数不能为零."))         {             currentText="0.";             text.setText(currentText);         }                           if(command=="BackSpace")         {             if(currentText.length()>1)                 currentText=currentText.substring(0,currentText.length()-1);             else if(currentText.length()<=1)             {                 currentText="0.";                              }             text.setText(currentText);         }                  if(command=="CE")         {             text.setText("0.");             op2=0.0;             //return;         }                  if(command=="C")         {             text.setText("0.");             op3=0.0;             op1=0.0;             op2=0.0;             op2x=0.0;             op=0;             opx=0;             opp=0;             firstdeng=true;             b=false;             c=false;                     }                  if(command=="0")         {             if(text.getText().equals("0."))             {                 text.setText("0.");                 b=true;             }             else                 text.setText(currentText+"0");         }                  if(command=="1")         {             if(c)             {                 c=false;                 text.setText("1");                 return;             }             if((text.getText().equals("0.") || text.getText().equals("0")) & !b)                 text.setText("1");             else                 text.setText(currentText+"1");                      }                  if(command=="2")         {             if(c)             {                 text.setText("2");                 c=false;                 return;             }             if((text.getText().equals("0.") || text.getText().equals("0")) & !b)                 text.setText("2");             else                 text.setText(currentText+"2");             //return;         }         if(command=="3")         {             if(c)             {                 c=false;                 text.setText("3");                 return;             }             if((text.getText().equals("0.") || text.getText().equals("0")) & !b)                 text.setText("3");             else                 text.setText(currentText+"3");             //return;         }         if(command=="4")         {             if(c)             {                 c=false;                 text.setText("4");                 return;             }             if((text.getText().equals("0.") || text.getText().equals("0")) & !b)                 text.setText("4");             else                 text.setText(currentText+"4");             //return;         }         if(command=="5")         {             if(c)             {                 c=false;                 text.setText("5");                 return;             }             if((text.getText().equals("0.") || text.getText().equals("0")) & !b)                 text.setText("5");             else                 text.setText(currentText+"5");             //return;         }         if(command=="6")         {             if(c)             {                 c=false;                 text.setText("6");                 return;             }             if((text.getText().equals("0.") || text.getText().equals("0")) & !b)                 text.setText("6");             else                 text.setText(currentText+"6");             //return;         }         if(command=="7")         {             if(c)             {                 c=false;                 text.setText("7");                 return;             }             if((text.getText().equals("0.") || text.getText().equals("0")) & !b)                 text.setText("7");             else                 text.setText(currentText+"7");             //return;         }         if(command=="8")         {             if(c)             {                 c=false;                 text.setText("8");                 return;             }             if((text.getText().equals("0.") || text.getText().equals("0")) & !b)                 text.setText("8");             else                 text.setText(currentText+"8");             //return;         }         if(command=="9")         {             if(c)             {                 c=false;                 text.setText("9");                 return;             }             if((text.getText().equals("0.") || text.getText().equals("0")) & !b)                 text.setText("9");             else                 text.setText(currentText+"9");                      }                  if(command=="/")         {                             op2=Double.valueOf(text.getText()).doubleValue();             if(opp!='=')             {                 op1=calculateNow(op1,op,op2);                 opp='/';             }             op='/';             opx='/';             text.setText(String.valueOf(op1));             c=true;             //b=false;             firstdeng=true;            

           }                  if(command=="*")         {             op2=Double.valueOf(text.getText()).doubleValue();               if(opp!='=')             {                 op1=calculateNow(op1,op,op2);                 opp='*';             }             op='*';             opx='*';             text.setText(String.valueOf(op1));             c=true;             firstdeng=true;                      }                       if(command=="-")         {                         op2=Double.valueOf(text.getText()).doubleValue();             if(opp!='=')             {                 op1=calculateNow(op1,op,op2);                 opp='-';             }             op='-';             opx='-';             text.setText(String.valueOf(op1));             c=true;             firstdeng=true;         }         if(command=="+")         {             op2=Double.valueOf(text.getText()).doubleValue();             if(opp!='=')             {                 op1=calculateNow(op1,op,op2);                 opp='+';             }             op='+';             opx='+';             text.setText(String.valueOf(op1));             c=true;             firstdeng=true;         }                  if(command==".")         {             if(text.getText().indexOf('.')>1)                 return;             else if(text.getText().indexOf('.')==-1)             {                 text.setText(currentText+".");              b=true;             }             else if(text.getText().indexOf('.')==1)                 b=true;                      }        

         if(command=="+/-")         {             if(Double.valueOf(text.getText()).doubleValue()!=0)             {                 double d=-Double.valueOf(text.getText()).doubleValue();                           text.setText(String.valueOf(d));              op2=d;              op2x=d;             }         }                  if(command=="%")         {             op2=Double.valueOf(text.getText()).doubleValue();             if(op1!=0 & op2!=0)             {                 double temp=op1*op2;                 temp=temp/100;                 text.setText(String.valueOf(temp));             }                                 }                      if(command=="sqrt")         {             double in=Double.valueOf(text.getText()).doubleValue();             if(in<0)                 text.setText("函数输入无效.");             else             {                 double sd=Math.sqrt(in);                 text.setText(String.valueOf(sd));                 op2=sd;              op2x=sd;             }

         }                  if(command=="1/x")         {             double in=Double.valueOf(text.getText()).doubleValue();             if(in==0)                 text.setText("除数不能为零.");             else             {                 double sd=1/in;                 text.setText(String.valueOf(sd));                 op2=sd;              op2x=sd;             }         }                  if(command=="=")         {             op2=Double.valueOf(text.getText()).doubleValue();             System.out.println("=========="+op2);             if(firstdeng)             {                 op2x=op2;                 firstdeng=false;             }                          op1=calculateNow(op1,op,op2x);             opp='=';             text.setText(String.valueOf(op1));         }                  if(command=="MC")         {             mjia=0;             text2.setText("");         }         if(command=="MR")         {             text.setText(String.valueOf(mjia));         }         if(command=="MS")         {             mjia=Double.valueOf(text.getText()).doubleValue();             text2.setText("M");         }         if(command=="M+")         {             mjia=mjia+Double.valueOf(text.getText()).doubleValue();         }              }

    }}
相关内容
赞助商链接