posts - 4,  comments - 0,  trackbacks - 0
          import java.awt.*;
          import java.awt.event.*;
          import javax.swing.*;

          public class MyCalculator extends JFrame implements ActionListener{

              MyPanel mp;
              ResPanel rp;
              
          boolean pflag = false// 判斷輸入框里面是否有“."
              String re = ""// 初始顯示框里面的內容
              double temp=0;  //暫存點擊 + - * /前的內容
              int op=0;    //0為= 1為+ 2為- 3為* 4為/ 記住操作類型
              public static void main(String[] args) {
                  
          // TODO Auto-generated method stub

                  MyCalculator myCalculator 
          = new MyCalculator();

              }


              
          public MyCalculator() {

                  rp 
          = new ResPanel();
                  mp 
          = new MyPanel();
                  mp.one.addActionListener(
          this);
                  mp.two.addActionListener(
          this);
                  mp.three.addActionListener(
          this);
                  mp.four.addActionListener(
          this);
                  mp.five.addActionListener(
          this);
                  mp.six.addActionListener(
          this);
                  mp.seven.addActionListener(
          this);
                  mp.eight.addActionListener(
          this);
                  mp.nine.addActionListener(
          this);
                  mp.zero.addActionListener(
          this);
                  mp.tzero.addActionListener(
          this);
                  mp.point.addActionListener(
          this);
                  mp.plus.addActionListener(
          this);
                  mp.minus.addActionListener(
          this);
                  mp.mul.addActionListener(
          this);
                  mp.div.addActionListener(
          this);
                  mp.del.addActionListener(
          this);
                  mp.ce.addActionListener(
          this);
                  mp.equal.addActionListener(
          this);
                  
          this.add(rp, BorderLayout.NORTH);
                  
          this.add(mp, BorderLayout.CENTER);
                  
          this.setTitle("Make By Stone");
                  
          this.setSize(280200);
                  
          this.setResizable(false);
                  
          this.setLocation(200200);
                  
          this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  
          this.setVisible(true);
              }


              @Override
              
          public void actionPerformed(ActionEvent e) {
                  
          // TODO Auto-generated method stub
                  re = rp.text.getText(); // 取出顯示框里面的內容
                  if (e.getSource() == mp.one) {

                      
          if (re.equals("0")) // 判斷
                      {
                          rp.text.setText(
          "1");
                      }
           else {
                          rp.text.setText(re 
          + "1");
                      }

                  }

                  
          if (e.getSource() == mp.two) {

                      
          if (re.equals("0")) {
                          rp.text.setText(
          "2");
                      }
           else {
                          rp.text.setText(re 
          + "2");
                      }

                  }

                  
          if (e.getSource() == mp.three) {

                      
          if (re.equals("0")) {
                          rp.text.setText(
          "3");
                      }
           else {
                          rp.text.setText(re 
          + "3");
                      }

                  }

                  
          if (e.getSource() == mp.four) {

                      
          if (re.equals("0")) {
                          rp.text.setText(
          "4");
                      }
           else {
                          rp.text.setText(re 
          + "4");
                      }

                  }

                  
          if (e.getSource() == mp.five) {

                      
          if (re.equals("0")) {
                          rp.text.setText(
          "5");
                      }
           else {
                          rp.text.setText(re 
          + "5");
                      }

                  }

                  
          if (e.getSource() == mp.six) {

                      
          if (re.equals("0")) {
                          rp.text.setText(
          "6");
                      }
           else {
                          rp.text.setText(re 
          + "6");
                      }

                  }

                  
          if (e.getSource() == mp.seven) {

                      
          if (re.equals("0")) {
                          rp.text.setText(
          "7");
                      }
           else {
                          rp.text.setText(re 
          + "7");
                      }

                  }


                  
          if (e.getSource() == mp.eight) {

                      
          if (re.equals("0")) {
                          rp.text.setText(
          "8");
                      }
           else {
                          rp.text.setText(re 
          + "8");
                      }

                  }

                  
          if (e.getSource() == mp.nine) {

                      
          if (re.equals("0")) {
                          rp.text.setText(
          "9");
                      }
           else {
                          rp.text.setText(re 
          + "9");
                      }

                  }

                  
          if (e.getSource() == mp.zero) {

                      
          if (re.equals("0")) {
                          rp.text.setText(
          "0");
                      }
           else {
                          rp.text.setText(re 
          + "0");
                      }

                  }

                  
          if (e.getSource() == mp.tzero) {

                      
          if (re.equals("0.0"|| re.equals("0")) {
                          rp.text.setText(
          "0");
                      }
           else {
                          rp.text.setText(re 
          + "00");
                      }

                  }

                  
          if (e.getSource() == mp.point) {
                       
                      
          if (pflag) {

                      }
           else {
                          
          if (re.equals("0")) {
                              rp.text.setText(
          "0.");
                          }
           else {
                              rp.text.setText(re 
          + ".");
                          }

                      }

                      pflag 
          = true;
                  }

                  
          if (e.getSource() == mp.del) {

                      rp.text.setText(re.substring(
          0, re.length()-1));
                  }

                  
          if (e.getSource() == mp.ce) {
                      temp
          =0;
                      rp.text.setText(
          "0");
                  }

                  
          if (e.getSource() == mp.plus) {

                      
          if(op!=0){
                          
          this.calculate();
                          op
          =1;
                          temp
          =Double.valueOf(rp.text.getText());
                          rp.text.setText(
          "0");
                      }
          else{
                          op
          =1;
                          temp
          =Double.valueOf(rp.text.getText());
                          rp.text.setText(
          "0");
                      }

                      
                  }

                  
          if (e.getSource() == mp.minus) {

                      
          if(op!=0){
                          
          this.calculate();
                          op
          =2;
                          temp
          =Double.valueOf(rp.text.getText());
                          rp.text.setText(
          "0");
                      }
          else{
                          op
          =2;
                          temp
          =Double.valueOf(rp.text.getText());
                          rp.text.setText(
          "0");
                      }

                      
                  }

                  
          if (e.getSource() == mp.mul) {

                      
          if(op!=0){
                          
          this.calculate();
                          op
          =3;
                          temp
          =Double.valueOf(rp.text.getText());
                          rp.text.setText(
          "0");
                      }
          else{
                          op
          =3;
                          temp
          =Double.valueOf(rp.text.getText());
                          rp.text.setText(
          "0");
                      }

                      
                  }

                  
          if (e.getSource() == mp.div) {

                      
          if(op!=0){
                          
          this.calculate();
                          op
          =4;
                          temp
          =Double.valueOf(rp.text.getText());
                          rp.text.setText(
          "0");
                      }
          else{
                          op
          =4;
                          temp
          =Double.valueOf(rp.text.getText());
                          rp.text.setText(
          "0");
                      }

                      
                  }

                  
          if (e.getSource() == mp.equal) {
                      
          this.calculate();
                  }


              }

              
          public void calculate(){
                  String s
          =rp.text.getText();
                  
          double temp2=Double.valueOf(s);
                  
          switch(op){
                  
          case 0:
                      rp.text.setText(String.valueOf(temp2));
                      
          break;
                  
          case 1:
                      temp
          =temp+temp2;
                      rp.text.setText(String.valueOf(temp));
                      op
          =0;
                      
          break;
                  
          case 2:
                      temp
          =temp-temp2;
                      rp.text.setText(String.valueOf(temp));
                      op
          =0;
                      
          break;
                  
          case 3:
                      temp
          =temp*temp2;
                      rp.text.setText(String.valueOf(temp));
                      op
          =0;
                      
          break;
                  
          case 4:
                      temp
          =temp/temp2;
                      rp.text.setText(String.valueOf(temp));
                      op
          =0;
                      
          break;
                  }

              }

          }


          class ResPanel extends JPanel {
              JTextField text;

              
          public ResPanel() {
                  text 
          = new JTextField(20);
                  text.setHorizontalAlignment(SwingConstants.RIGHT);
                  text.setText(
          "0");
                  text.enable(
          false);
                  
          this.add(text);
              }

          }


          class MyPanel extends JPanel {
              JButton zero 
          = new JButton("0");
              JButton one 
          = new JButton("1");
              JButton two 
          = new JButton("2");
              JButton three 
          = new JButton("3");
              JButton four 
          = new JButton("4");
              JButton five 
          = new JButton("5");
              JButton six 
          = new JButton("6");
              JButton seven 
          = new JButton("7");
              JButton eight 
          = new JButton("8");
              JButton nine 
          = new JButton("9");
              JButton plus 
          = new JButton("+");
              JButton minus 
          = new JButton("-");
              JButton mul 
          = new JButton("*");
              JButton div 
          = new JButton("/");
              JButton point 
          = new JButton(".");
              JButton tzero 
          = new JButton("00");
              JButton equal 
          = new JButton("=");
              JButton ce 
          = new JButton("CE");
              JButton del 
          = new JButton("");
              JButton mFor 
          = new JButton("YH");

              
          public MyPanel() {
                  JPanel jp1 
          = new JPanel();
                  JPanel jp2 
          = new JPanel();
                  JPanel jp3 
          = new JPanel();
                  jp1.setLayout(
          new GridLayout(4455));
                  jp1.add(one);
                  jp1.add(two);
                  jp1.add(three);
                  jp1.add(four);
                  jp1.add(five);
                  jp1.add(six);
                  jp1.add(seven);
                  jp1.add(eight);
                  jp1.add(nine);
                  jp1.add(zero);
                  jp1.add(tzero);
                  jp1.add(point);
                  jp2.setLayout(
          new GridLayout(4355));
                  jp2.add(plus);
                  jp2.add(minus);
                  jp2.add(mul);
                  jp2.add(div);
                  jp2.add(del);
                  jp2.add(ce);
                  jp2.add(equal);
                  jp2.add(mFor);
                  jp3.add(jp1, BorderLayout.WEST);
                  jp3.add(jp2, BorderLayout.EAST);
                  
          this.add(jp3, BorderLayout.CENTER);
              }

          }
          [img]/Files/shanrenxj/images/myCalculator.jpg[/img][  源文件:/Files/shanrenxj/源文件/MyCalculator.zip
          posted on 2012-01-03 16:12 愛燕神鷹 閱讀(69) 評論(0)  編輯  收藏

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


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 樟树市| 吉林省| 宿迁市| 定边县| 射阳县| 德安县| 迁安市| 远安县| 安图县| 清原| 淮南市| 裕民县| 河南省| 鄂托克旗| 武冈市| 宜君县| 武定县| 甘肃省| 迁安市| 平乡县| 江都市| 九江县| 汝城县| 邵阳县| 利津县| 万载县| 新绛县| 昌黎县| 贞丰县| 宁河县| 蓬安县| 万载县| 温宿县| 南昌县| 孟连| 龙海市| 精河县| 塔河县| 清涧县| 永清县| 五常市|