與Java相伴的日子
          相識,相知,相戀,到相守......我的日子因你的到來而充實,我的日子因你的存在而多姿!
          posts - 4,comments - 31,trackbacks - 0
          ??????? 這是我初學Java時用Java編寫的小型計算器,現在看來,確實有點幼稚,不過,畢竟是自己寫的,放在這里,權作紀念吧,嘿嘿。
          ?????? 代碼如下:
          import?java.awt.*;
          import?java.awt.event.*;
          public?class?Calculator?extends?Frame
          {
          ?TextField?text?
          =?new?TextField(20);
          ?Panel?p
          =new?Panel();
          ?
          double?num;???????????//存放操作數
          ?String?op;
          ?
          double?result;????????//存放計算結果
          ?int?idx=0;????????????//用于退格鍵
          ?boolean?flag=false;//用于追加數字還是重新顯示
          ?boolean?dbClick=false;//是否連按"="號
          ?public?Calculator()
          ?
          {
          ??
          super("簡易計算器");
          ??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)
          ???
          {//設置按鈕前景色
          ????button[i].setForeground(Color.red);
          ???}

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

          ???button[i].setFont(
          new?Font("宋體",Font.BOLD,15));
          ???button[i].addActionListener(
          new?MyActionListener());//添加監聽器
          ???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) 評論(1)  編輯  收藏

          FeedBack:
          # re: 我用Java編的一個小計算器
          2007-11-11 15:17 | 忽忽
          不錯了!怎么說幼稚呢?太謙虛了!  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 南川市| 渭源县| 逊克县| 诏安县| 忻州市| 福贡县| 离岛区| 晋宁县| 安康市| 崇信县| 习水县| 濮阳市| 屏南县| 正镶白旗| 晋城| 旺苍县| 宜章县| 迁西县| 东乡族自治县| 汾西县| 建水县| 遂平县| 通辽市| 三亚市| 苏尼特右旗| 宿迁市| 芷江| 阿拉善左旗| 南华县| 府谷县| 武城县| 宜阳县| 正阳县| 江城| 扎兰屯市| 南平市| 石家庄市| 娄烦县| 鲜城| 兴海县| 洱源县|