wlyBlack
wlyBlack
采纳率0%
2016-11-23 09:51

Java计算器的完整代码

用Java怎么写一个计算器啊,监听器不会,按钮用循环在运行时不能显示,键盘有字符的输入、、、、、、求完整代码

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • qq_32823759 qq_32823759 5年前

    import java.awt.BorderLayout;
    import java.awt.Font;
    import java.awt.Frame;
    import java.awt.GridLayout;
    import java.awt.Rectangle;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;

    import exampleFrame.Calculator2;

    public class Calcutor4 extends JFrame implements ActionListener{

    // 标志用户按的是否是整个表达式的第一个数字,或者是运算符后的第一个数字
    private boolean firstDigit = true;
    // 计算的中间结果。
    private double resultNum = 0.0;
    // 当前运算的运算符
    private String operator = "=";
    // 操作是否合法
    private boolean operateValidFlag = true;
    
        JPanel contentPane;
        JPanel contentPane2;
        JPanel contentPane3;
    
        JTextField tex = new JTextField("0");
        JButton a1 = new JButton("MC");
        JButton a2 = new JButton("MR");
        JButton a3 = new JButton("MS");
        JButton a4 = new JButton("M+");
        JButton a5 = new JButton("M-");
        JButton b1 = new JButton("←");
        JButton b2 = new JButton("CE");
        JButton b3 = new JButton("C");
        JButton b4 = new JButton("±");
        JButton b5 = new JButton("√");
        JButton c1 = new JButton("7");
        JButton c2 = new JButton("8");
        JButton c3 = new JButton("9");
        JButton c4 = new JButton("/");
        JButton c5 = new JButton("%");
        JButton d1 = new JButton("4");
        JButton d2 = new JButton("5");
        JButton d3 = new JButton("6");
        JButton d4 = new JButton("*");
        JButton d5 = new JButton("½");
        JButton e1 = new JButton("1");
        JButton e2 = new JButton("2");
        JButton e3 = new JButton("3");
        JButton e4 = new JButton("-");
        JButton f1 = new JButton("=");
        JButton f2 = new JButton("0");
        JButton f3 = new JButton(".");
        JButton f4 = new JButton("+");
    
    
        public Calcutor4() {
    
    
            //主面板
            contentPane  = (JPanel)getContentPane();
            contentPane.setLayout(null);
            this.setResizable(false);
            setSize(310, 399);
            setTitle("计算器");
    
            tex.setEnabled(false);
            tex.setFont(new java.awt.Font("Dialog", Font.PLAIN, 20));
            tex.setHorizontalAlignment(SwingConstants.RIGHT);
            tex.setEditable(false);
            tex.setBounds(new Rectangle(20, 10, 260, 30));
            //设置jpane2
            contentPane2 = (JPanel)new JPanel();
            contentPane2.setLayout(new GridLayout(4,5,5,5));
            contentPane2.setBounds(20,50,260, 200);
    
            a1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            a2.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            a3.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            a4.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            a5.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            b1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            b2.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            b3.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            b4.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            b5.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            c1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            c2.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            c3.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            c4.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            c5.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            d1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            d2.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            d3.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            d4.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            d5.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
    
            contentPane2.add(a1);
            contentPane2.add(a2);
            contentPane2.add(a3);
            contentPane2.add(a4);
            contentPane2.add(a5);
            contentPane2.add(b1);
            contentPane2.add(b2);
            contentPane2.add(b3);
            contentPane2.add(b4);
            contentPane2.add(b5);
            contentPane2.add(c1);
            contentPane2.add(c2);
            contentPane2.add(c3);
            contentPane2.add(c4);
            contentPane2.add(c5);
            contentPane2.add(d1);
            contentPane2.add(d2);
            contentPane2.add(d3);
            contentPane2.add(d4);
            contentPane2.add(d5);
    
            //设置面板3
            contentPane3 = new JPanel();
            contentPane3.setLayout(null);
            contentPane3.setBounds(20,255,260, 200);
    
            e1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            e2.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            e3.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            e4.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            f1.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            f2.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            f3.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
            f4.setFont(new java.awt.Font("宋体", Font.PLAIN, 12));
    
            e1.setBounds(new Rectangle(0, 0, 48, 45));
            e2.setBounds(new Rectangle(53,0, 48, 45));
            e3.setBounds(new Rectangle(106, 0, 48, 45));
            e4.setBounds(new Rectangle(159, 0, 48, 45));
            f1.setBounds(new Rectangle(212, 0, 48, 100));
            f2.setBounds(new Rectangle(0, 53, 101, 45));
            f3.setBounds(new Rectangle(106, 53, 48, 45));
            f4.setBounds(new Rectangle(159, 53, 48, 45));
    
            contentPane3.add(e1);
            contentPane3.add(e2);
            contentPane3.add(e3);
            contentPane3.add(e4);
            contentPane3.add(f1);
            contentPane3.add(f2);
            contentPane3.add(f3);
            contentPane3.add(f4);
    
           //添加事件监听器
    
            c1.addActionListener(this);
            c2.addActionListener(this);
            c3.addActionListener(this);
            c4.addActionListener(this);
            d1.addActionListener(this);
            d2.addActionListener(this);
            d3.addActionListener(this);
            d4.addActionListener(this);
            e1.addActionListener(this);
            e2.addActionListener(this);
            e3.addActionListener(this);
            e4.addActionListener(this);
            b1.addActionListener(this);
            b3.addActionListener(this);
            f1.addActionListener(this);
            f2.addActionListener(this);
            f3.addActionListener(this);
            f4.addActionListener(this);
    
    
    
    
            contentPane.add(contentPane3);
            contentPane.add(contentPane2);
            contentPane.add(tex);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    
        }
    
    
    
    
        public static void main(String[] args) {
            Calcutor4 calculator3 = new Calcutor4();
        }
    
    
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // 获取事件源的标签
            String label = e.getActionCommand();
    
            System.out.println(label);
            if (label.equals("←")) {
                // 用户按了"Backspace"键
                handleBackspace();
    
            } else if (label.equals("C")) {
                // 用户按了"C"键
                handleC();
            } else if ("0123456789.".indexOf(label) >= 0) {
                // 用户按了数字键或者小数点键
                handleNumber(label);
            } else {
                // 用户按了运算符键
                handleOperator(label);
            }
        }
    
        private void handleBackspace() {
            String text = tex.getText();
            int i = text.length();
            if (i > 0) {
                // 退格,将文本最后一个字符去掉
                text = text.substring(0, i - 1);
                if (text.length() == 0) {
                    // 如果文本没有了内容,则初始化计算器的各种值
                    tex.setText("0");
                    firstDigit = true;
                    operator = "=";
                } else {
                    // 显示新的文本
                    tex.setText(text);
                }
            }
        }
    
        /**
         * 处理数字键被按下的事件
         * 
         * @param key
         */
        private void handleNumber(String key) {
    
            if (firstDigit) {
                // 输入的第一个数字
                tex.setText(key);
            } else if ((key.equals(".")) && (tex.getText().indexOf(".") < 0)) {
                // 输入的是小数点,并且之前没有小数点,则将小数点附在结果文本框的后面
                tex.setText(tex.getText() + ".");
            } else if (!key.equals(".")) {
                // 如果输入的不是小数点,则将数字附在结果文本框的后面
                tex.setText(tex.getText() + key);
            }
            // 以后输入的肯定不是第一个数字了
            firstDigit = false;
        }
    
        /**
         * 处理C键被按下的事件
         */
        private void handleC() {
            // 初始化计算器的各种值
            tex.setText("0");
            firstDigit = true;
            operator = "=";
        }
    
        /**
         * 处理运算符键被按下的事件
         * 
         * @param key
         */
        private void handleOperator(String key) {
            if (operator.equals("/")) {
                // 除法运算
                // 如果当前结果文本框中的值等于0
                if (getNumberFromText() == 0.0) {
                    // 操作不合法
                    operateValidFlag = false;
                    tex.setText("除数不能为零");
                } else {
                    resultNum /= getNumberFromText();
                }
            } else if (operator.equals("+")) {
                // 加法运算
                resultNum += getNumberFromText();
            } else if (operator.equals("-")) {
                // 减法运算
                resultNum -= getNumberFromText();
            } else if (operator.equals("*")) {
                // 乘法运算
            } else if (operator.equals("=")) {
                // 赋值运算
                resultNum = getNumberFromText();
            }
            if (operateValidFlag) {
                // 双精度浮点数的运算
                long t1;
                double t2;
                t1 = (long) resultNum;
                t2 = resultNum - t1;
                if (t2 == 0) {
                    tex.setText(String.valueOf(t1));
                } else {
                    tex.setText(String.valueOf(resultNum));
                }
            }
            // 运算符等于用户按的按钮
            operator = key;
            firstDigit = true;
            operateValidFlag = true;
        }
    
        /**
         * 从结果文本框中获取数字
         * 
         * @return
         */
        private double getNumberFromText() {
            double result = 0;
            try {
                result = Double.valueOf(tex.getText()).doubleValue();
            } catch (NumberFormatException e) {
            }
            return result;
        }
    

    }

    点赞 评论 复制链接分享
  • qq_25827845 温柔狠角色 5年前

    首先:你的有一个静态界面,其实就是普通的java图形化界面开发。
    其次:你可以给按钮添加监听。
    然后:触发计算加减乘除的函数,得到返回结果。并将结果append在应该显示的组件中。

    关于监听的设置方法:

    public class Login extends JFrame implements ActionListener

       //创建组件  
        jb1=new JButton("登录");  
        jb2=new JButton("重置");  
        //设置监听  
        jb1.addActionListener(this);  
        jb2.addActionListener(this);  
    
    
                 @Override  
    public void actionPerformed(ActionEvent e) {  
    
        if(e.getActionCommand()=="登录")  
        {  
            //如果选中教师登录  
            if(jrb1.isSelected())  
            {  
                  tealogin();  
            }else if(jrb2.isSelected()) //学生在登录系统  
            {  
                  stulogin();  
            }  
    
        }else if(e.getActionCommand()=="重置")  
        {  
                  clear();  
        }             
    
    }
    
    点赞 评论 复制链接分享

为你推荐