Java实现的计算器(完整源码)


import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;


public class Calculator extends JFrame {

    private JTextField jt = new JTextField(250);

    double a;
    double b;
    boolean isRun = false;
    String s = "";
    String num = "";
    String run = null;



    public Calculator(){

        setLayout(null);
        setBounds(0,0,300,300);
        Container c = getContentPane();

        //添加组件
        jt.setBounds(5,5,285,30);
        c.add(jt);
        jt.setText(null);

        JButton b1 = new JButton("7");
        JButton b2 = new JButton("8");
        JButton b3 = new JButton("9");
        JButton b4 = new JButton("4");
        JButton b5 = new JButton("5");
        JButton b6 = new JButton("6");
        JButton b7 = new JButton("1");
        JButton b8 = new JButton("2");
        JButton b9 = new JButton("3");
        JButton b10 = new JButton(".");
        JButton b11 = new JButton("0");
        JButton b12 = new JButton("");
        JButton b13 = new JButton("+");
        JButton b14 = new JButton("-");
        JButton b15 = new JButton("*");
        JButton b16 = new JButton("/");
        JButton b17 = new JButton("C");
        JButton b18 = new JButton("x!");
        JButton b19 = new JButton("x");
        JButton b20 = new JButton("=");

        b1.setBounds(10, 50, 50, 30);
        b2.setBounds(65, 50, 50, 30);
        b3.setBounds(120, 50, 50, 30);
        b4.setBounds(10, 85, 50, 30);
        b5.setBounds(65, 85, 50, 30);
        b6.setBounds(120, 85, 50, 30);
        b7.setBounds(10, 120, 50, 30);
        b8.setBounds(65, 120, 50, 30);
        b9.setBounds(120, 120, 50, 30);
        b10.setBounds(10, 155, 50, 30);
        b11.setBounds(65, 155, 50, 30);
        b12.setBounds(120, 155, 50, 30);
        b13.setBounds(180, 50, 50, 30);
        b14.setBounds(180, 85, 50, 30);
        b15.setBounds(180, 120, 50, 30);
        b16.setBounds(180, 155, 50, 30);
        b17.setBounds(235, 50, 50, 30);
        b18.setBounds(235, 85, 50, 30);
        b19.setBounds(235, 120, 50, 30);
        b20.setBounds(235, 155, 50, 30);

        c.add(b1);
        c.add(b2);
        c.add(b3);
        c.add(b4);
        c.add(b5);
        c.add(b6);
        c.add(b7);
        c.add(b8);
        c.add(b9);
        c.add(b10);
        c.add(b11);
        c.add(b12);
        c.add(b13);
        c.add(b14);
        c.add(b15);
        c.add(b16);
        c.add(b17);
        c.add(b18);
        c.add(b19);
        c.add(b20);

        //按钮事件
        //数字
        ////////////////////////////////////////////////////////////
        b1.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                jt.setText("7");
                num = num+jt.getText();
                jt.setText(num);

                jt.requestFocus();
            }   
        });
        b2.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                jt.setText("8");
                num = num+jt.getText();
                jt.setText(num);

                jt.requestFocus();
            }   
        });
        b3.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                jt.setText("9");
                num = num+jt.getText();
                jt.setText(num);

                jt.requestFocus();
            }   
        });
        b4.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                jt.setText("4");
                num = num+jt.getText();
                jt.setText(num);

                jt.requestFocus();
            }   
        });
        b5.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                jt.setText("5");
                num = num+jt.getText();
                jt.setText(num);

                jt.requestFocus();
            }   
        });
        b6.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                jt.setText("6");
                num = num+jt.getText();
                jt.setText(num);

                jt.requestFocus();
            }   
        });
        b7.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                jt.setText("1");
                num = num+jt.getText();
                jt.setText(num);

                jt.requestFocus();
            }   
        });
        b8.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                jt.setText("2");
                num = num+jt.getText();
                jt.setText(num);

                jt.requestFocus();
            }   
        });
        b9.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                jt.setText("3");
                num = num+jt.getText();
                jt.setText(num);

                jt.requestFocus();
            }   
        });
        b10.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                if(jt.getText()!=null){
                    jt.setText(".");
                    num = num+jt.getText();
                    jt.setText(num);
                }else{
                    JOptionPane.showMessageDialog(null,"第一位数字不能是小数点");
                }

                jt.requestFocus();
            }   
        });
        b11.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub

                jt.setText("0");
                num = num+jt.getText();
                jt.setText(num);

                jt.requestFocus();
            }   
        });



        /////////////////////////////////////////////////////////////////
        //运算符
        /////////////////////////////////////////////////////////////////
        b13.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                if(!isRun){
                    jt.setText("+");
                    s = num;
                    num="";
                    run = "+";
                }else{
                    //jt.setText(Double.toString(a));
                    if(run.equals("+")){
                        b = Double.valueOf(s)+Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "+";
                    }else if(run.equals("-")){
                        b = Double.valueOf(s)-Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "+"; 
                    }else if(run.equals("*")){
                        b = Double.valueOf(s)*Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "+"; 
                    }else if(run.equals("/")){
                        b = Double.valueOf(s)/Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "+"; 
                    }else if(run.equals("^")){
                        b = Math.pow(Double.valueOf(s),Double.valueOf(num));
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "+"; 
                    }else if(run.equals("=")){
                        jt.setText("+");
                        s = Double.toString(a);
                        num = "";
                        run = "+"; 
                    }
                }

                isRun = true;

                jt.requestFocus();
            }   
        });
        b14.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                if(!isRun){
                    jt.setText("-");
                    s = num;
                    num="";
                    run = "-";
                }else{
                    if(run.equals("+")){
                        b = Double.valueOf(s)+Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "-"; 
                    }else if(run.equals("-")){
                        b = Double.valueOf(s)-Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "-"; 
                    }else if(run.equals("*")){
                        b = Double.valueOf(s)*Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "-"; 
                    }else if(run.equals("/")){
                        b = Double.valueOf(s)/Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "-"; 
                    }else if(run.equals("^")){
                        b = Math.pow(Double.valueOf(s),Double.valueOf(num));
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "-"; 
                    }else if(run.equals("=")){
                        jt.setText("-");
                        s = Double.toString(a);
                        num = "";
                        run = "-"; 
                    }
                }
                isRun = true;

                jt.requestFocus();
            }   
        });
        b15.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                if(!isRun){
                    jt.setText("*");
                    s = num;
                    num="";
                    run = "*";
                }else{
                    //JOptionPane.showMessageDialog(null, "不能连续输入运算符");
                    if(run.equals("+")){
                        b = Double.valueOf(s)+Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "*"; 
                    }else if(run.equals("-")){
                        b = Double.valueOf(s)-Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "*"; 
                    }else if(run.equals("*")){
                        b = Double.valueOf(s)*Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "*"; 
                    }else if(run.equals("/")){
                        b = Double.valueOf(s)/Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "*"; 
                    }else if(run.equals("^")){
                        b = Math.pow(Double.valueOf(s),Double.valueOf(num));
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "*"; 
                    }else if(run.equals("=")){
                        jt.setText("*");
                        s = Double.toString(a);
                        num = "";
                        run = "*"; 
                    }
                }
                isRun = true;

                jt.requestFocus();
            }   
        });
        b16.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                if(!isRun){
                    jt.setText("/");
                    s = num;
                    num="";
                    run = "/";
                }else{
                    //JOptionPane.showMessageDialog(null, "不能连续输入运算符");
                    if(run.equals("+")){
                        b = Double.valueOf(s)+Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "/"; 
                    }else if(run.equals("-")){
                        b = Double.valueOf(s)-Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "/"; 
                    }else if(run.equals("*")){
                        b = Double.valueOf(s)*Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "/"; 
                    }else if(run.equals("/")){
                        b = Double.valueOf(s)/Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "/"; 
                    }else if(run.equals("^")){
                        b = Math.pow(Double.valueOf(s),Double.valueOf(num));
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "/"; 
                    }else if(run.equals("=")){
                        jt.setText("/");
                        s = Double.toString(a);
                        num = "";
                        run = "/"; 
                    }
                }
                isRun = true;

                jt.requestFocus();
            }   
        });
        //阶乘
        b18.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub 
                if(!isRun){
                    s = num;
                    double c;
                    c = Double.valueOf(s);
                    if(((c%2==0)||(c%2==1))&&c>0){
                        jt.setText("x!");
                        num="";
                        run = "x!";
                    }else{
                        JOptionPane.showMessageDialog(null, "非正整数不能进行阶乘运算");
                    }

                }else{
                    //JOptionPane.showMessageDialog(null, "不能连续输入运算符");
                    if(run.equals("=")){
                        //System.out.println(a);//测试
                        if(((a%2==0)||(a%2==1))&&a>0){
                            jt.setText("x!");
                            s = Double.toString(a);
                            num = "";
                            run = "x!"; 
                            System.out.println(s);//测试
                        }else{
                            JOptionPane.showMessageDialog(null,"非正整数不能进行阶乘运算");
                        }

                    }   


                }
                isRun = true;

                jt.requestFocus();
            }   
        });
        //指数运算
        b19.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                if(!isRun){
                    jt.setText("^");
                    s = num;
                    num="";
                    run = "^";
                }else{
                    //JOptionPane.showMessageDialog(null, "不能连续输入运算符");
                    if(run.equals("+")){
                        b = Double.valueOf(s)+Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "^"; 
                    }else if(run.equals("-")){
                        b = Double.valueOf(s)-Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "^"; 
                    }else if(run.equals("*")){
                        b = Double.valueOf(s)*Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "^"; 
                    }else if(run.equals("/")){
                        b = Double.valueOf(s)/Double.valueOf(num);
                        s = Double.toString(b);
                        jt.setText(s);
                        num = "";
                        run = "^"; 
                    }else if(run.equals("=")){
                        jt.setText("^");
                        s = Double.toString(a);
                        num = "";
                        run = "^"; 
                    }
                }
                isRun = true;

                jt.requestFocus();
            }   
        });

        //等于
        b20.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                if(run.equals("+")){
                    a = Double.valueOf(s)+Double.valueOf(num);
                }else if(run.equals("-")){
                    a = Double.valueOf(s)-Double.valueOf(num); 
                }else if(run.equals("*")){
                    a = Double.valueOf(s)*Double.valueOf(num); 
                }else if(run.equals("/")){
                    a = Double.valueOf(s)/Double.valueOf(num); 
                }else if(run.equals("x!")){
// double check = Double.parseDouble(s);
// check = (int)check;
// int c,result;
// int d = (int)check;
                    double c,result;
                    double d = Double.valueOf(s);
                    result = c = d;
                    for(int i=1;i<d;i++){
                        result = result*(c-i);
                    }
                    a = result;
                }else if(run.equals("^")){
                    a = Math.pow(Double.valueOf(s),Double.valueOf(num));
                    //a = Double.valueOf(s)^Double.valueOf(num); 
                }

                jt.setText(Double.toString(a)); 
                isRun = true;
                run = "=";

                jt.requestFocus();
            }   
        });
        ///////////////////////////////////////////////////////////////// 
        //归零
        b17.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                jt.setText("");
                num = "";
                s = "";
                isRun = false;
                run = null;

                jt.requestFocus();
            }   
        });     
    }   
    /** * @param args */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        init(new Calculator(),310,250);
    }

    private static void init(Calculator frame, int width, int height) {
        // TODO Auto-generated method stub
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setSize(width,height);
        frame.setTitle("计算器");
        frame.setVisible(true);
    }
}

你可能感兴趣的:(阶乘,计算器,指数运算)