java吧 关注:1,271,390贴子:12,780,735
  • 9回复贴,共1

这是个简易计算器的代码,有问题以我的水平改不出来!求大虾帮忙!

只看楼主收藏回复

import java.awt.BorderLayout;import java.awt.Container;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JPanel;import javax.swing.JTextField;public class Calculator extends JFrame implements ActionListener { boolean init = true; boolean isMath = false; boolean clear = true; boolean clickable = true; double qian; String fuhao; int all = 0; JTextField text = new JTextField(25); JButton bM = new JButton(); JButton bMC = new JButton("MC"); JButton bMR = new JButton("MR"); JButton bMS = new JButton("MS"); JButton bMADD = new JButton("M+"); JButton b0 = new JButton("0"); JButton b1 = new JButton("1"); JButton b2 = new JButton("2"); JButton b3 = new JButton("3"); JButton b4 = new JButton("4"); JButton b5 = new JButton("5"); JButton b6 = new JButton("6"); JButton b7 = new JButton("7"); JButton b8 = new JButton("8"); JButton b9 = new JButton("9"); JButton bNOP = new JButton("+/-"); JButton bDot = new JButton("."); JButton bDiv = new JButton("/"); JButton bMul = new JButton("*"); JButton bSub = new JButton("-"); JButton bAdd = new JButton("+"); JButton bSprt = new JButton("sprt"); JButton bMod = new JButton("%"); JButton bDao = new JButton("1/x"); JButton bEqual = new JButton("="); JButton bBackspace = new JButton("Backspace"); JButton bCE = new JButton("CE"); JButton bC = new JButton("C");
public Calculator() { this.setTitle("计算器"); JMenuBar mainMenu = new JMenuBar(); setJMenuBar(mainMenu); JMenu editMenu = new JMenu("编辑"); JMenu viewMenu = new JMenu("查看"); JMenu helpMenu = new JMenu("帮助"); mainMenu.add(editMenu); mainMenu.add(viewMenu); mainMenu.add(helpMenu);
JPanel jpDisplay = new JPanel(); JPanel jpInput = new JPanel(); JPanel jpLeft = new JPanel(); JPanel jpRight = new JPanel();
text.setText("0."); text.setHorizontalAlignment(JTextField.RIGHT); jpDisplay.add(text);
bM.addActionListener(this); bMC.addActionListener(this); bMS.addActionListener(this); bMR.addActionListener(this); bMADD.addActionListener(this);
jpLeft.setLayout(new GridLayout(5, 1)); jpLeft.add(bM); jpLeft.add(bMC); jpLeft.add(bMR); jpLeft.add(bMS); jpLeft.add(bMADD);
JPanel jpInnerN = new JPanel(); JPanel jpInnerS = new JPanel();
bBackspace.addActionListener(this); bCE.addActionListener(this); bC.addActionListener(this);
jpInnerN.setLayout(new GridLayout(1, 3)); jpInnerN.add(bBackspace); jpInnerN.add(bCE); jpInnerN.add(bC);
b0.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); bNOP.addActionListener(this); bDot.addActionListener(this); bDiv.addActionListener(this); bMul.addActionListener(this); bSub.addActionListener(this); bAdd.addActionListener(this); bSprt.addActionListener(this); bMod.addActionListener(this); bDao.addActionListener(this); bEqual.addActionListener(this);



1楼2011-11-29 12:09回复
    jpInnerS.setLayout(new GridLayout(4, 5)); jpInnerS.add(b7); jpInnerS.add(b8); jpInnerS.add(b9); jpInnerS.add(bDiv); jpInnerS.add(bSprt); jpInnerS.add(b4); jpInnerS.add(b5); jpInnerS.add(b6); jpInnerS.add(bMul); jpInnerS.add(bMod); jpInnerS.add(b1); jpInnerS.add(b2); jpInnerS.add(b3); jpInnerS.add(bSub); jpInnerS.add(bDao); jpInnerS.add(b0); jpInnerS.add(bNOP); jpInnerS.add(bDot); jpInnerS.add(bAdd); jpInnerS.add(bEqual);
    jpRight.setLayout(new BorderLayout()); jpRight.add(jpInnerN, BorderLayout.NORTH); jpRight.add(jpInnerS, BorderLayout.CENTER);
    jpInput.setLayout(new BorderLayout()); jpInput.add(jpLeft, BorderLayout.WEST); jpInput.add(jpRight, BorderLayout.CENTER);
    Container pane = this.getContentPane();
    pane.setSize(333, 208); this.setLocation(300, 200); this.setLayout(new BorderLayout());
    pane.add(jpDisplay, BorderLayout.CENTER); pane.add(jpInput, BorderLayout.SOUTH);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.pack(); this.setVisible(true); } public void actionPerformed(ActionEvent e) { if (init) this.text.setText("0."); init = false;
    Object source = e.getSource();
    if (source == bBackspace) { String s = text.getText(); text.setText(""); for (int i = 0; i < s.length() - 1; i++) { char a = s.charAt(i); text.setText(text.getText() + a); } System.out.println("backspace"); } if (source == bCE) { text.setText("0."); clear = true; init = true;
    System.out.println("CE"); } if (source == bC) { text.setText("0."); clear = true; init = true; System.out.println("C"); } if (source == bM) { System.out.println("M"); } if (source == bMC) { System.out.println("MC,功能末实现"); } if (source == bMR) { System.out.println("MR,功能末实现"); } if (source == bMS) { System.out.println("MS,功能末实现"); } if (source == bMADD) { System.out.println("M+,功能末实现"); } if (source == b0) { System.out.println("0"); if (clear == false)// 判断是否点击了符号位 text.setText(""); text.setText(text.getText() + "0");
    } if (source == b1) { if (clear == false) text.setText(""); text.setText(text.getText() + "1"); clear = true;// 第二次不在清空(前二句) } if (source == b2) { System.out.println("2"); if (clear == false) text.setText(""); text.setText(text.getText() + "2"); clear = true; } if (source == b3) { System.out.println("3"); if (clear == false) text.setText(""); text.setText(text.getText() + "3"); clear = true; } if (source == b4) { System.out.println("4"); if (clear == false) text.setText(""); text.setText(text.getText() + "4"); clear = true; } if (source == b5) { System.out.println("5"); if (clear == false) text.setText(""); text.setText(text.getText() + "5"); clear = true; } if (source == b6) { System.out.println("6"); if (clear == false) text.setText(""); text.setText(text.getText() + "6"); clear = true; } if (source == b7) { System.out.println("7"); if (clear == false) text.setText(""); text.setText(text.getText() + "7"); clear = true; } if (source == b8) { System.out.println("8"); if (clear == false) text.setText(""); text.setText(text.getText() + "8"); clear = true; } if (source == b9) { System.out.println("9"); if (clear == false) text.setText(""); text.setText(text.getText() + "9"); clear = true;
    


    2楼2011-11-29 12:09
    回复
      2025-07-27 21:51:58
      广告
      不感兴趣
      开通SVIP免广告
      倒...怎么成这样了...


      4楼2011-11-29 12:09
      回复

        import
        java.awt.BorderLayout;
        import
        java.awt.Container;
        import
        java.awt.GridBagConstraints;
        import
        java.awt.GridBagLayout;
        import
        java.awt.GridLayout;
        import
        java.awt.Insets;
        import
        java.awt.event.ActionEvent;
        import
        java.awt.event.ActionListener;
        import
        java.awt.event.MouseEvent;
        import
        java.awt.event.MouseListener;
        import
        javax.swing.JButton;
        import
        javax.swing.JFrame;
        import
        javax.swing.JMenu;
        import
        javax.swing.JMenuBar;
        import
        javax.swing.JPanel;
        import
        javax.swing.JTextField;
        public class
        Calculator extends JFrame implements ActionListener {
        boolean init = true;
        boolean isMath = false;
        boolean clear = true;
        boolean clickable = true;
        double qian;
        String fuhao;
        int all = 0;
        JTextField text = new JTextField(25);
        JButton bM = new JButton();
        JButton bMC = new JButton("MC");
        JButton bMR = new JButton("MR");
        JButton bMS = new JButton("MS");
        JButton bMADD = new JButton("M+");
        JButton b0 = new JButton("0");
        JButton b1 = new JButton("1");
        JButton b2 = new JButton("2");
        JButton b3 = new JButton("3");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("7");
        JButton b8 = new JButton("8");
        JButton b9 = new JButton("9");
        JButton bNOP = new JButton("+/-");
        JButton bDot = new JButton(".");
        JButton bDiv = new JButton("/");
        JButton bMul = new JButton("*");
        JButton bSub = new JButton("-");
        JButton bAdd = new JButton("+");
        JButton bSprt = new
        JButton("sprt");
        JButton bMod = new JButton("%");
        JButton bDao = new JButton("1/x");
        JButton bEqual = new JButton("=");
        JButton bBackspace = new JButton("Backspace");
        JButton bCE = new JButton("CE");
        JButton bC = new JButton("C");
        public Calculator() {
        this.setTitle("计算器");
        JMenuBar mainMenu = new JMenuBar();
        setJMenuBar(mainMenu);
        JMenu editMenu = new JMenu("编辑");
        JMenu viewMenu = new JMenu("查看");
        JMenu helpMenu = new JMenu("帮助");
        mainMenu.add(editMenu);
        mainMenu.add(viewMenu);
        mainMenu.add(helpMenu);
        JPanel jpDisplay = new JPanel();
        JPanel jpInput = new JPanel();
        JPanel jpLeft = new JPanel();
        


        5楼2011-11-29 12:11
        回复

          JPanel jpRight = new JPanel();
          text.setText("0.");
          text.setHorizontalAlignment(JTextField.RIGHT);
          jpDisplay.add(text);
          bM.addActionListener(this);
          bMC.addActionListener(this);
          bMS.addActionListener(this);
          bMR.addActionListener(this);
          bMADD.addActionListener(this);
          jpLeft.setLayout(new GridLayout(5, 1));
          jpLeft.add(bM);
          jpLeft.add(bMC);
          jpLeft.add(bMR);
          jpLeft.add(bMS);
          jpLeft.add(bMADD);
          JPanel jpInnerN = new JPanel();
          JPanel jpInnerS = new JPanel();
          bBackspace.addActionListener(this);
          bCE.addActionListener(this);
          bC.addActionListener(this);
          jpInnerN.setLayout(new GridLayout(1, 3));
          jpInnerN.add(bBackspace);
          jpInnerN.add(bCE);
          jpInnerN.add(bC);
          b0.addActionListener(this);
          b1.addActionListener(this);
          b2.addActionListener(this);
          b3.addActionListener(this);
          b4.addActionListener(this);
          b5.addActionListener(this);
          b6.addActionListener(this);
          b7.addActionListener(this);
          b8.addActionListener(this);
          b9.addActionListener(this);
          bNOP.addActionListener(this);
          bDot.addActionListener(this);
          bDiv.addActionListener(this);
          bMul.addActionListener(this);
          bSub.addActionListener(this);
          bAdd.addActionListener(this);
          bSprt.addActionListener(this);
          bMod.addActionListener(this);
          bDao.addActionListener(this);
          bEqual.addActionListener(this);
          jpInnerS.setLayout(new GridLayout(4, 5));
          jpInnerS.add(b7);
          jpInnerS.add(b8);
          jpInnerS.add(b9);
          jpInnerS.add(bDiv);
          jpInnerS.add(bSprt);
          jpInnerS.add(b4);
          jpInnerS.add(b5);
          jpInnerS.add(b6);
          jpInnerS.add(bMul);
          jpInnerS.add(bMod);
          jpInnerS.add(b1);
          jpInnerS.add(b2);
          jpInnerS.add(b3);
          jpInnerS.add(bSub);
          jpInnerS.add(bDao);
          jpInnerS.add(b0);
          jpInnerS.add(bNOP);
          jpInnerS.add(bDot);
          jpInnerS.add(bAdd);
          jpInnerS.add(bEqual);
          jpRight.setLayout(new BorderLayout());
          


          6楼2011-11-29 12:11
          回复
            jpRight.add(jpInnerN,
            BorderLayout.NORTH);
            jpRight.add(jpInnerS,
            BorderLayout.CENTER);
            jpInput.setLayout(new BorderLayout());
            jpInput.add(jpLeft, BorderLayout.WEST);
            jpInput.add(jpRight,
            BorderLayout.CENTER);
            Container pane = this.getContentPane();
            pane.setSize(333, 208);
            this.setLocation(300, 200);
            this.setLayout(new BorderLayout());
            pane.add(jpDisplay, BorderLayout.CENTER);
            pane.add(jpInput, BorderLayout.SOUTH);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.pack();
            this.setVisible(true);
            }
            public void actionPerformed(ActionEvent e) {
            if (init)
            this.text.setText("0.");
            init = false;
            Object source = e.getSource();
            if (source == bBackspace) {
            String s = text.getText();
            text.setText("");
            for (int i = 0; i < s.length() - 1;
            i++) {
            char a = s.charAt(i);
            text.setText(text.getText() + a);
            }
            System.out.println("backspace");
            }
            if (source == bCE) {
            text.setText("0.");
            clear = true;
            init = true;
            System.out.println("CE");
            }
            if (source == bC) {
            text.setText("0.");
            clear = true;
            init = true;
            System.out.println("C");
            }
            if (source == bM) {
            System.out.println("M");
            }
            if (source == bMC) {
            System.out.println("MC,功能末实现");
            }
            if (source == bMR) {
            System.out.println("MR,功能末实现");
            }
            if (source == bMS) {
            System.out.println("MS,功能末实现");
            }
            if (source == bMADD) {
            System.out.println("M+,功能末实现");
            }
            if (source == b0) {
            System.out.println("0");
            if (clear == false)// 判断是否点击了符号位
            text.setText("");
            text.setText(text.getText() +
            "0");
            }
            if (source == b1) {
            if (clear == false)
            text.setText("");
            text.setText(text.getText() +
            "1");
            clear = true;// 第二次不在清空(前二句)
            


            7楼2011-11-29 12:11
            回复

              }
              if (source == b2) {
              System.out.println("2");
              if (clear == false)
              text.setText("");
              text.setText(text.getText() +
              "2");
              clear = true;
              }
              if (source == b3) {
              System.out.println("3");
              if (clear == false)
              text.setText("");
              text.setText(text.getText() +
              "3");
              clear = true;
              }
              if (source == b4) {
              System.out.println("4");
              if (clear == false)
              text.setText("");
              text.setText(text.getText() +
              "4");
              clear = true;
              }
              if (source == b5) {
              System.out.println("5");
              if (clear == false)
              text.setText("");
              text.setText(text.getText() +
              "5");
              clear = true;
              }
              if (source == b6) {
              System.out.println("6");
              if (clear == false)
              text.setText("");
              text.setText(text.getText() +
              "6");
              clear = true;
              }
              if (source == b7) {
              System.out.println("7");
              if (clear == false)
              text.setText("");
              text.setText(text.getText() +
              "7");
              clear = true;
              }
              if (source == b8) {
              System.out.println("8");
              if (clear == false)
              text.setText("");
              text.setText(text.getText() +
              "8");
              clear = true;
              }
              if (source == b9) {
              System.out.println("9");
              if (clear == false)
              text.setText("");
              text.setText(text.getText() +
              "9");
              clear = true;
              }
              try{
              if (source == bNOP) {
              System.out.println("+/-");
              boolean isNumber = true;
              String s = text.getText();
              for (int i = 0; i < s.length();
              i++)
              if (!(s.charAt(i) >= '0'
              && s.charAt(i) <= '9'|| s.charAt(i) == '.' || s.charAt(i) == '-')) {
              isNumber = false;
              break;
              }
              if (isNumber == true) {
              // 如果当前字符串首字母有'-'号,代表现在是个负数,再按下时,则将首符号去掉
              if (s.charAt(0) == '-') {
              


              8楼2011-11-29 12:11
              回复
                '0'
                &&
                text.getText().length() == 1) {
                text.setText("除数不能为零");
                } else {
                boolean isDec = true;
                int i, j, k;
                String s= Double.toString(1
                / Double.parseDouble(text.getText()));
                for (i = 0; i < s.length();
                i++)
                if (s.charAt(i) == '.')
                break;
                for (j = i + 1; j < s.length();
                j++)
                if (s.charAt(j) != '0') {
                isDec = false;
                break;
                }
                if (isDec == true) {
                String stemp = "";
                for (k = 0; k < i; k++)
                stemp += s.charAt(k);
                text.setText(stemp);
                } else
                text.setText(s);
                }
                clear = false;
                }
                if (source == bEqual) {
                System.out.println("=");
                Double ss=Double.parseDouble(text.getText());
                text.setText("");
                if (fuhao == "+")
                text.setText(qian + ss +
                "");
                if (fuhao == "-")
                text.setText(qian - ss +
                "");
                if (fuhao == "*")
                text.setText(qian * ss +
                "");
                if (fuhao == "/")
                text.setText(qian
                / ss + "");
                clear = false;// 要清空前一次的数据
                ;
                }
                }catch (Exception ee) {
                System.out.println("请正确输入");
                text.setText("运算出错,给您带来不便,sorry");
                clear = false;
                }
                }
                public static void main(String[] args) {
                new Calculator();
                }
                }


                10楼2011-11-29 12:11
                回复
                  2025-07-27 21:45:58
                  广告
                  不感兴趣
                  开通SVIP免广告
                  我很想知道这个论坛能不能和 csdn 一类的专业论坛一样贴出代码风格样式的代码……
                  楼主贴的这样子,怎么看啊……


                  IP属地:江苏11楼2011-11-29 12:29
                  回复
                    看到这代码,一个头两个大,直接晕死了…………………………


                    12楼2011-11-30 13:21
                    回复