與Java相伴的日子
          相識(shí),相知,相戀,到相守......我的日子因你的到來而充實(shí),我的日子因你的存在而多姿!
          posts - 4,comments - 31,trackbacks - 0
          ??????? 這是我初學(xué)Java時(shí)用Java編寫的小型計(jì)算器,現(xiàn)在看來,確實(shí)有點(diǎn)幼稚,不過,畢竟是自己寫的,放在這里,權(quán)作紀(jì)念吧,嘿嘿。
          ?????? 代碼如下:
          import?java.awt.*;
          import?java.awt.event.*;
          public?class?Calculator?extends?Frame
          {
          ?TextField?text?
          =?new?TextField(20);
          ?Panel?p
          =new?Panel();
          ?
          double?num;???????????//存放操作數(shù)
          ?String?op;
          ?
          double?result;????????//存放計(jì)算結(jié)果
          ?int?idx=0;????????????//用于退格鍵
          ?boolean?flag=false;//用于追加數(shù)字還是重新顯示
          ?boolean?dbClick=false;//是否連按"="號(hào)
          ?public?Calculator()
          ?
          {
          ??
          super("簡(jiǎn)易計(jì)算器");
          ??String[]?name??
          ={"退格","CE","C","/","7","8","9","*","4","5","6","-",
          ??????
          "1","2","3","+","0","+/-",".","="}
          ;??????
          ??Button[]?button
          =?new??Button[name.length];
          ??add(text,
          "North");
          ??text.setFont(
          new?Font("宋體",Font.BOLD,20));
          ??text.setText(
          "0.0");
          ??text.setEditable(
          false);
          ??text.setFocusable(
          false);
          ??
          ??p.setLayout(
          new?GridLayout(5,4,2,2));??
          ??
          for?(int?i=0;i<name.length;i++)
          ??
          {
          ???button[i]
          =new?Button(name[i]);
          ???
          if(i<=3||i==7||i==11||i==15||i==19)
          ???
          {//設(shè)置按鈕前景色
          ????button[i].setForeground(Color.red);
          ???}

          ???
          else
          ???
          {
          ????button[i].setForeground(Color.blue);
          ???}

          ???button[i].setFont(
          new?Font("宋體",Font.BOLD,15));
          ???button[i].addActionListener(
          new?MyActionListener());//添加監(jiān)聽器
          ???p.add(button[i]);?
          ??}
          ??
          ??add(p,
          "South");
          ??setSize(
          250,200);
          ??
          this.setLocation(300,300);
          ??
          this.setResizable(false);//窗體不可改變大小??
          ??addWindowListener(new?WindowAdapter()
          ??
          {
          ???
          public?void?windowClosing(WindowEvent?e)
          ???
          {
          ????System.exit(
          0);
          ???}

          ??}
          );??
          ??
          this.setVisible(true);
          ?}

          ?
          class?MyActionListener?implements?ActionListener
          ?
          {
          ??
          public?void?actionPerformed(ActionEvent?e)
          ??
          {
          ???String?input
          =text.getText();
          ???String?str
          =e.getActionCommand();
          ???
          if(isNum(str))
          ???
          {
          ????
          if(!flag)
          ????
          {
          ?????
          if(input!=""&&!input.equals("0.0")&&!input.equals("0"))
          ?????
          {
          ??????text.setText(input
          +str);
          ?????}

          ?????
          else
          ?????
          {?
          ??????text.setText(str);
          ?????}

          ????}

          ????
          else
          ????
          {?????
          ?????text.setText(str);
          ????}

          ????flag
          =false;???
          ???}

          ??????
          else?if(str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/"))
          ???
          {
          ????result
          =Double.parseDouble(input);
          ????op
          =str;
          ????flag
          =true;
          ????dbClick
          =false;
          ???}

          ??????
          if(str.equals("="))
          ???
          {
          ????
          if(dbClick==false)
          ????
          {
          ?????num
          =Double.parseDouble(input);
          ????}
          ???????????
          ????
          if(op.equals("+"))
          ????
          {
          ?????result
          +=num;
          ????}

          ????
          if(op.equals("-"))
          ????
          {
          ?????result
          -=num;
          ????}

          ????
          if(op.equals("*"))
          ????
          {
          ?????result
          *=num;
          ????}

          ????
          if(op.equals("/"))
          ????
          {
          ?????
          if(num!=0.0)
          ?????
          {
          ??????result
          /=num;?????
          ?????}
          ????????
          ????}

          ????text.setText(
          ""+result);
          ????flag
          =false;
          ????dbClick
          =true;
          ???}

          ???
          if(str.equals("CE"))
          ???
          {
          ????text.setText(
          "0.0");????
          ????num
          =0;???
          ???}

          ???
          if(str.equals(".")&&input.indexOf(".")<0)
          ???
          {
          ????text.setText(input
          +".");????
          ???}

          ???
          if(str.equals("+/-"))
          ???
          {
          ????
          double?m?=?Double.parseDouble(input);
          ????
          if(m!=0.0)
          ????
          {
          ?????text.setText(m
          *(-1)+"");
          ????}

          ???}

          ???
          if(str.equals("C"))
          ???
          {
          ????text.setText(
          "0.0");
          ????flag
          =false;
          ????dbClick
          =false;
          ????result
          =0;
          ????num
          =0;
          ???}

          ???
          if(str.equals("退格"))
          ???
          {
          ????idx
          =input.length()-1;
          ????
          if(idx<0)
          ????
          {
          ?????
          return;
          ????}

          ????text.setText(input.substring(
          0,idx));
          ???}

          ???
          ??}

          ?}

          ?
          public?boolean?isNum(String?str)
          ?
          {
          ??
          if(str.equals("0")||str.equals("1")||str.equals("2")||str.equals("3")||str.equals("4")
          ???
          ||str.equals("5")||str.equals("6")||str.equals("7")||str.equals("8")||str.equals("9"))
          ??
          {
          ???
          return?true;
          ??}

          ??
          else
          ??
          {
          ???
          return?false;
          ??}

          ?}

          ?
          public?static?void?main(String[]?args)
          ?
          {
          ??
          new?Calculator();
          ?}

          }
          posted on 2006-04-15 00:52 南一郎 閱讀(975) 評(píng)論(1)  編輯  收藏

          FeedBack:
          # re: 我用Java編的一個(gè)小計(jì)算器
          2007-11-11 15:17 | 忽忽
          不錯(cuò)了!怎么說幼稚呢?太謙虛了!  回復(fù)  更多評(píng)論
            

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 天津市| 新沂市| 灌云县| 安化县| 辽阳市| 济源市| 禄劝| 行唐县| 肃北| 宜良县| 温宿县| 城市| 县级市| 贵定县| 绥宁县| 克什克腾旗| 新民市| 皋兰县| 江源县| 宁远县| 隆德县| 高淳县| 嘉鱼县| 伊宁县| 鄂托克前旗| 新竹县| 阳城县| 鹤峰县| 永昌县| 藁城市| 聊城市| 道孚县| 霍林郭勒市| 宁国市| 平南县| 雷州市| 湛江市| 太仓市| 天镇县| 青神县| 定陶县|