今天是:
 
您现在的 位置: 浩特个人网 >> 文章频道 >> 编程源码 >> JAVA源代码 >> 正文 本站公告:
注册会员体验更多精彩!  [2006-08-17 09:54:52]      浩特个人网介绍  [2006-08-17 09:52:30]      浩特个人网全新改版!  [2006-07-24 13:27:58]
  JAVA小计算器    5星级
JAVA小计算器
[ 作者:无     来源:搜集整理     点击数:     更新时间:2006-05-30     文章录入:
【字体: 字体颜色
不才,小弟学一个月JAVA写的一个老师作业,JAVA小计算器,还很薄弱,难以抗拒"不法用户"的使用,先传上来供大虾们笑笑!

//Calculater(程序员是像写情书一样写代码)
import java.awt.*;
import java.awt.event.*;

class MainPanel extends Panel  //Screen Panle class
{
    TextField scn=new TextField("");  //TextField screen
    String str[]={"7","8","9","*","4","5","6","-","1","2","3","+","0",".","/","="};
    Button btn[]=new Button[str.length];
    String temp,op,tempInKey,keyDown;
    char opChar;
    public float num1,num2;
    boolean flag;

    MainPanel()
    {
        Panel ScreenPanel = new Panel();  //Screen Panel
        //scn; //Welcome to Calculater!
        ScreenPanel.setLayout(new BorderLayout());//Screen in the north
        ScreenPanel.add(scn,BorderLayout.CENTER);  
        scn.disable();  //can not receive event
        keyInput kPut=new keyInput();  //keyAction
        scn.addKeyListener(kPut);

        Panel ButtonPanel = new Panel();   //Button Panel
        ButtonPanel.setLayout(new GridLayout(4,4));
        for (int i=0;i<16;i++)
        {
            btn[i]=new Button(str[i]);  //new Button
            ButtonPanel.add(btn[i]);  //add to panel
            btn[i].addActionListener(new lsn());
            btn[i].addKeyListener(kPut);  //buttons be listened
        }
        
        //scn.addKeyListener(kPut);
        
        //Add to Main Panel
        
        setLayout(new BorderLayout()); //Main panel
        add(ScreenPanel,BorderLayout.NORTH);
        add(ButtonPanel,BorderLayout.CENTER);
    }        
    
    class lsn implements ActionListener  //listener
    {
        public void actionPerformed(ActionEvent e)
        {
            temp=scn.getText();   //save the numbers on screen first
            //judge
            if (flag)
                {
                    temp= "";    //clear the numbers on the screen
                    flag=false;
                }
            if (e.getActionCommand()=="+" |e.getActionCommand()=="-"
                    |e.getActionCommand()=="*" |e.getActionCommand()=="/")
                {                       //if operation clicked
                        if (scn.getText().equals("") )  //等于时没有内容的处理
                             return;
                
                        num1=Float.parseFloat(scn.getText());  
                        op=e.getActionCommand();  //operation type
                        scn.setText("");
                }
            else if (e.getActionCommand()=="=")  //calculate
                {
                 try{
                         if (scn.getText().equals("") )  //等于时没有内容的处理
                             return;
                        
                        num2=Float.parseFloat(scn.getText());
                        if (op=="+")
                            num2=num1+num2;
                        else if (op=="-")
                            num2=num1-num2;
                        else if (op=="*")
                            num2=num1*num2;
                    
                        else if    (op=="/")
                        {
                            //if(num2 < 0.0000001 & num2>0.00000001)
                            //{
                            //   scn.setText("0不能做除数");
                            //   flag=true;
                        //       return;
                        //    }  
                            num2=num1/num2;
                        }
                        scn.setText(String.valueOf(num2));
                        flag=true;
                    }
                    catch(ArithmeticException ex)
                    {
                         System.out.println(ex.toString());
                    }
                }
            //else if (e.getActionCommand()=="CLS")  //clear
            //    scn.setText("");
            else   //add to screen
                scn.setText(temp+e.getActionCommand());  //setText
        }
    }
    
    class keyInput extends KeyAdapter
    {
        public void keyPressed (KeyEvent k)
        {
            if ((k.getKeyCode()>47 & k.getKeyCode()<58) | (k.getKeyCode()>96 & k.getKeyCode()<112)
                        |k.getKeyCode()==10)
            {
                tempInKey=scn.getText();  //num1
                keyDown=String.valueOf(k.getKeyChar());  //char to String
                System.out.print(k.getKeyChar());
                if (flag)
                    {
                        tempInKey= "";    //clear the numbers on the screen
                        flag=false;
                    }
                if (k.getKeyChar()=='+'|k.getKeyChar()=='-'|k.getKeyChar()=='*'|
                        k.getKeyChar()=='/')
                    {
                        if (scn.getText().equals("") )  //等于时没有内容的处理
                                 return;
                         num1=Float.parseFloat(scn.getText());
                        opChar=k.getKeyChar();
                        scn.setText("");
                    }
                else if (k.getKeyChar()=='='|k.getKeyCode()==10)  //calculate
                {            
                 try{
                     System.out.println(opChar);
                         if (scn.getText().equals("") )  //等于时没有内容的处理
                             return;
                        num2=Float.parseFloat(scn.getText());
                        if (opChar=='+')
                            num2=num1+num2;
                        else if (opChar=='-')
                            num2=num1-num2;
                        else if (opChar=='*')
                            num2=num1*num2;
                    
                        else if    (opChar=='/')
                        {
                            //if(num2 < 0.0000001 & num2>0.00000001)
                            //{
                            //   scn.setText("0不能做除数");
                             //  flag=true;
                            //   return;
                            //}  
                            num2=num1/num2;
                        }
                        scn.setText(String.valueOf(num2));
                        flag=true;
                    }
                    catch(ArithmeticException ex)
                    {
                        System.out.println(ex.toString());
                    }
                }
                else
                    scn.setText(tempInKey+keyDown);  //set
            }
            else
                return;
        }
    }
}
        
class CalcFrame extends Frame   //construct a frame
{
    public CalcFrame(String title)
    {
        super(title);
        MainPanel pScr=new MainPanel(); //Main Panel
        add(pScr);   //add MainFrame
        addWindowListener(new end());  //add listener
    }
    
    class end extends WindowAdapter  //close the window,inner class
    {
        public void windowClosing(WindowEvent e)
        {
            System.exit(0);
        }
    }
}

class Calculater  //main()
{
    public static void main(String args[])
    {
        CalcFrame frm=new CalcFrame("JAVA计算器");
        frm.setSize(220,180); //new frame,go above
        frm.show();    
    }
}
  • 上一篇文章: JAVA程序__计算器

  • 下一篇文章: java编写TCP方式的通信程序
  • 发表评论   告诉好友   打印此文  收藏此页  关闭窗口  返回顶部
     最新5篇热点文章
     面对面有时不一定是相遇(...
     Windows Vista 主题安装包...
     两首《你到底爱谁》歌词:...
     四首《童话》歌词:光良、...
     情人节手机短信搞笑大全
     
     最新5篇推荐文章
     面对面有时不一定是相遇(...
     现在的女孩子满脑子都是色...
     千万别找太聪明的美眉做女...
     fireworks logo教程
     电话英语生活应用12大类
     
     相 关 文 章

      网友评论:(只显示最新5条。评论内容只代表网友观点,与本站立场无关!)


    制作:笨超 Copyright©2006-2007
    陇ICP备05005205号