实现一个计算器,界面包括10个数字按钮(0至9)和四个运算符(加减乘除)按钮,以及等号和清空两个辅助按钮,还有一个用于显示输入输出的文本框。

【+】

    JButton btnNewButton_12 = new JButton("+");

        btnNewButton_12.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                a = s.length();

                s = s + "+";

                textField.setText(s);

                x = Double.parseDouble(s.substring(0, a));

                c = '+';

            }

        });

【-】

    JButton btnNewButton_13 = new JButton("-");

        btnNewButton_13.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                a = s.length();

                s = s + "-";

                textField.setText(s);

                x = Double.parseDouble(s.substring(0, a));

                c = '-';

            }

        });

【x】

    JButton btnNewButton_14 = new JButton("x");

        btnNewButton_14.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                a = s.length();

                s = s + "x";

                textField.setText(s);

                x = Double.parseDouble(s.substring(0, a));

                c = 'x';

            }

        });

【/】

    JButton button = new JButton("/");

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                a = s.length();

                s = s + "/";

                x = Double.parseDouble(s.substring(0, a));

                textField.setText(s);

                c = '/';

            }

        });

【=】

    JButton btnNewButton = new JButton(" =  ");

        btnNewButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                b = s.length();

                y = Double.parseDouble(s.substring(a + 1, b));

 

                if (p[0] == c) {

                    double m = x + y;

                    String ss = String.valueOf(m);

                    textField_1.setText("=" + ss);

                }

                if (p[1] == c) {

                    double m = x - y;

                    String ss = String.valueOf(m);

                    textField_1.setText("=" + ss);

                }

                if (p[2] == c) {

                    double m = x * y;

                    String ss = String.valueOf(m);

                    textField_1.setText("=" + ss);

                }

                if (p[3] == c) {

                    double m = x / y;

                    String ss = String.valueOf(m);

                    textField_1.setText("=" + ss);

                }

            }

        });

【clear】

    JButton btnNewButton_11 = new JButton("clear");

        btnNewButton_11.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                y = x = (double) 0;

                c = ' ';

                s = "";

                textField.setText(s);

                textField_1.setText(s);

            }

        });

【delete】

JButton btnNewButton_10 = new JButton("d");

        btnNewButton_10.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {

                s = s.substring(0, s.length() - 1);

                textField.setText(s);

            }

        });


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值