Gathering_pace
月下观星
采纳率100%
2019-06-22 01:01

我在试着写一个计算器但是目前不知道怎么实现,只写了一个界面,希望能帮我补完这个代码。

已采纳

目前代码是这样的

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Calculator extends JFrame implements ActionListener{
    JFrame f = new JFrame("计算器");
    JTextField tf = new JTextField(18);
    JButton b0 = new JButton("7");
    JButton b1 = new JButton("8");
    JButton b2 = new JButton("9");
    JButton b3 = new JButton("X");
    JButton b4 = new JButton("4");
    JButton b5 = new JButton("5");
    JButton b6 = new JButton("6");
    JButton b7 = new JButton("-");
    JButton b8 = new JButton("1");
    JButton b9 = new JButton("2");
    JButton b10 = new JButton("3");
    JButton b11 = new JButton("+");
    JButton b12 = new JButton("+/-");
    JButton b13 = new JButton("0");
    JButton b14 = new JButton(".");
    JButton b15 = new JButton("=");
    JButton b16 = new JButton("%");
    JButton b17 = new JButton("÷");
    JButton b18 = new JButton("x²");
    JButton b19 = new JButton("x³");
    JButton b20 = new JButton("√");
    JButton b21 = new JButton("c");
    JButton b22 = new JButton("←");
    JButton b23 = new JButton("ce");
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();
    JPanel p5 = new JPanel();
    JPanel p6 = new JPanel();
    String nowButton;
    class MyListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            nowButton = arg0.getActionCommand();
            if(arg0.getActionCommand().equals("=")){
                tf.setText("");
            }

            if(arg0.getActionCommand().equals("c")){
                tf.setText("");
            }
            if (arg0.getActionCommand().equals("←")) {
                tf.setText(tf.getText() + nowButton);
            }
        }
    }



    private void init() {
        tf.setHorizontalAlignment(JTextField.RIGHT);
        p1.add(b23);
        p1.add(b17);
        p1.add(b21);
        p1.add(b22);
        p2.add(b0);
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p3.add(b4);
        p3.add(b5);
        p3.add(b6);
        p3.add(b7);
        p4.add(b8);
        p4.add(b9);
        p4.add(b10);
        p4.add(b11);
        p5.add(b12);
        p5.add(b13);
        p5.add(b14);
        p5.add(b15);
        p6.add(b16);
        p6.add(b18);
        p6.add(b19);
        p6.add(b20);
        Box box1 = Box.createVerticalBox();
        box1.add(p1);
        box1.add(p2);
        box1.add(p3);
        box1.add(p4);
        box1.add(p5);
        box1.add(p6);

        f.add(tf,BorderLayout.NORTH);
        f.add(box1,BorderLayout.SOUTH);
        f.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent arg0) {
                // TODO Auto-generated method stub
                super.windowClosing(arg0);
                System.exit(0);
            }
        });


        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        new Calculator().init();
    }

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

    }
}

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

1条回答

  • qq_42636652 clk_0.0 2年前

    package 计算器;
    //只能做整数运算,且还没有解决被除数为零时的情况,被除数为零时,会报错
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

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

    public class Demo extends JFrame {
    int temp1 = 0;
    int temp2 = 0;
    int ii = 0;
    String jbs;
    JTextField jtf;
    int countnumber = 0;
    String countcount = "";
    String[] numberarr = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
    String[] countarr = { "+", "-", "*", "/" };
    String c = "C";
    String dengyu = "=";

    public Demo() {
        setTitle("计算器");
        Dimension dsize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((int) (dsize.getWidth() / 2 - 260 / 2), (int) (dsize.getHeight() / 2 - 200 / 2), 200, 210);
        JPanel jp = new JPanel();
        this.setContentPane(jp);
        jtf = new JTextField(17);
        add(jtf);
        JButton jbc = new JButton(c);
        jp.add(jbc);
        jbc.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                jtf.setText("");
                countcount = "";
                countnumber = 0;
                ii = 0;
    
            }
        });
        for (int i = 0; i < numberarr.length; i++) {
            final JButton jb = new JButton(numberarr[i]);
            jp.add(jb);
            jb.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    String s = jb.getText();
                    jtf.setText(jtf.getText() + s);
    
                }
            });
        }
    
        JButton jbdengyu = new JButton(dengyu);
        jbdengyu.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent arg0) {
                numberCount2("=");
                countcount = "";
                countnumber = 0;
                ii = 0;
            }
        });
        jp.add(jbdengyu);
    
        for (int i = 0; i < countarr.length; i++) {
            final JButton jbcount = new JButton(countarr[i]);
            jp.add(jbcount);
            jbcount.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    jbs = jbcount.getText();
                    jtf.setText(jtf.getText() + jbs);
                    if (countnumber == 0) {
                        numberCount1(jbs);
                    } else if (countnumber >= 1) {
                        numberCount2(jbs);
                    }
    
                }
    
            });
        }
    
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public static void main(String[] args) {
        new Demo();
    }
    private void numberCount1(String jbs) {
        String temps = "";
        String arr = jtf.getText();
        temps = arr.substring(0, arr.length()-1);
        temp1 = Integer.parseInt(temps);
        countcount = jbs;
        countnumber++;
        ii = arr.length()-1;
    }
    private void numberCount2(String jbs) {
        String ss;
        if(jbs == "=") {
             ss = jtf.getText().substring(ii + 1, jtf.getText().length());
        }else {
             ss = jtf.getText().substring(ii + 1, jtf.getText().length()-1);
        }
        temp2 = Integer.parseInt(ss);
        switch (countcount) {
        case "+":
            if(jbs != "=") {
                jtf.setText(String.valueOf((temp1 + temp2)) + jbs);
                numberCount3(jbs);
                countnumber++;
            }else {
                jtf.setText(String.valueOf((temp1 + temp2)));
            }
            break;
        case "-":
            if(jbs != "=") {
                jtf.setText(String.valueOf((temp1 - temp2)) + jbs);
                numberCount3(jbs);
                countnumber++;
            }else {
                jtf.setText(String.valueOf((temp1 - temp2)));
            }
            break;
        case "*":
            if(jbs != "=") {
                jtf.setText(String.valueOf((temp1 * temp2)) + jbs);
                numberCount3(jbs);
                countnumber++;
            }else {
                jtf.setText(String.valueOf((temp1 * temp2)));
            }
            break;
        case "/":
            if(jbs != "=") {
                jtf.setText(String.valueOf((temp1 / temp2)) + jbs);
                numberCount3(jbs);
                countnumber++;
            }else {
                jtf.setText(String.valueOf((temp1 / temp2)));
            }
            break;
        default:
            break;
        }
    
    }
    private void numberCount3(String jbs) {
        String newtext = jtf.getText();
        temp1 = Integer.parseInt(newtext.substring(0, newtext.length()-1));
        ii = newtext.length()-1;
        countcount = jbs;
    }
    

    }

    我自己写的,你参考一下吧,具体思路我也不知道怎么说,大概是用一个数去记录当前文本框里的运算符,再没有碰见等于号或者C键时,就不进行运算,一旦有两个运算符出现,就先运算前面两个数,用结果加上第二个运算符显示在文本框中

    点赞 评论 复制链接分享

相关推荐