我无法让运营工作

时间:2016-04-16 18:28:49

标签: java swing calculator

我是初学者并试图制作一个简单的计算器。我已经搜索了其他答案,但我的问题是,在按下操作符后,它只保留第一个字符串

public class CalculatorFrame extends JFrame {

    public CalculatorFrame(){
        this.setTitle("Calculator");
        this.setLayout(new BorderLayout(2,2));

        MenuBar menuBar = new MenuBar();

        JTextArea txtArea = new JTextArea();
        txtArea.setEditable(false);
        txtArea.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        txtArea.setText("0");
        txtArea.setFont(new Font("Sans Serif", Font.PLAIN,30));

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(4,3,2,2));

        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(6,1,2,2));

        MyActionListener al = new MyActionListener(txtArea);
        MyOperatorListener ol =  new MyOperatorListener(txtArea);

        JButton btn = new JButton();
        int k=10;
        for (int i=0; i<3; i++) {
            k=k-3;
            for (int j=0; j<3; j++) {
                btn = new JButton(Integer.toString(k++));
                btn.setPreferredSize(new Dimension(70, 60));
                btn.setBackground(new Color(224,224,224));
                btn.setBorder(BorderFactory.createRaisedSoftBevelBorder());
                btn.setFont(new Font("Sans Serif", Font.BOLD,20));
                btn.addActionListener(al);
                panel.add(btn);
            }
            k=k-3;
        }

        JButton btn7 = new JButton("0");
        btn7.addActionListener(al);
        btn7.setBackground(new Color(224,224,224));
        btn7.setPreferredSize(new Dimension(70, 60));
        btn7.setBorder (BorderFactory.createRaisedSoftBevelBorder()) ;
        btn7.setFont(new Font("Sans Serif", Font.BOLD,20));

        JButton btn8 = new JButton(".");
        btn8.addActionListener(al);
        btn8.setBackground(new Color(224,224,224));
        btn8.setPreferredSize(new Dimension(70, 60));
        btn8.setBorder (BorderFactory.createRaisedSoftBevelBorder()) ;
        btn8.setFont(new Font("Sans Serif", Font.BOLD,20));

        JButton btn9 = new JButton("=");
        btn9.addActionListener(ol);
        btn9.setBackground(new Color(0,128,255));
        btn9.setBorder (BorderFactory.createEtchedBorder()) ;
        btn9.setPreferredSize(new Dimension(70, 60));
        btn9.setFont(new Font("Sans Serif", Font.BOLD,20));
        btn9.setToolTipText("calculate result");

        JButton btn1 = new JButton("CE");
        btn1.addActionListener(ol);
        btn1.setBackground(new Color(160,160,160));
        btn1.setPreferredSize(new Dimension(50, 40));
        btn1.setFont(new Font("Sans Serif", Font.BOLD,12));
        btn1.setToolTipText("clear entry");

        JButton btn2 = new JButton("C");
        btn2.addActionListener(ol);
        btn2.setBackground(new Color(160,160,160));
        btn2.setPreferredSize(new Dimension(50, 40));
        btn2.setFont(new Font("Sans Serif", Font.BOLD,15));
        btn2.setToolTipText("clear");

        JButton btn3 = new JButton("/");
        btn3.addActionListener(ol);
        btn3.setBackground(new Color(160,160,160));
        btn3.setPreferredSize(new Dimension(50, 40));
        btn3.setFont(new Font("Sans Serif", Font.BOLD,15));
        btn3.setToolTipText("divide");

        JButton btn4 = new JButton("*");
        btn4.addActionListener(ol);
        btn4.setBackground(new Color(160,160,160));
        btn4.setPreferredSize(new Dimension(50, 40));
        btn4.setFont(new Font("Sans Serif", Font.BOLD,15));
        btn4.setToolTipText("multiplie");

        JButton btn5 = new JButton("-");
        btn5.addActionListener(ol);
        btn5.setBackground(new Color(160,160,160));
        btn5.setPreferredSize(new Dimension(50, 40));
        btn5.setFont(new Font("Sans Serif", Font.BOLD,15));
        btn5.setToolTipText("subtract");

        JButton btn6 = new JButton("+");
        btn6.addActionListener(ol);
        btn6.setBackground(new Color(160,160,160));
        btn6.setPreferredSize(new Dimension(50, 40));
        btn6.setFont(new Font("Sans Serif", Font.BOLD,15));
        btn6.setToolTipText("add");

        this.add(txtArea, BorderLayout.NORTH);
        this.add(panel,BorderLayout.CENTER);
        this.add(panel2,BorderLayout.EAST);

        panel.add(btn7);
        panel.add(btn8);
        panel.add(btn9);
        panel2.add(btn1);
        panel2.add(btn2);
        panel2.add(btn3);
        panel2.add(btn4);
        panel2.add(btn5);
        panel2.add(btn6);


        this.setJMenuBar(menuBar);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setResizable(false);
        this.setVisible(true);
    }
}

public class MyActionListener implements ActionListener {

    JTextArea area;

    public MyActionListener(JTextArea area){
        this.area = area;
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        JButton x = (JButton) e.getSource();
        String y = x.getText();
        String z = area.getText();
        int v = area.getText().length();
        int i = z.indexOf('.');
        if(i==1 && "0".equals(area.getText())){
            area.setText("");
        }
        if(v<15){
            if(i>=0&&".".equals(y)){
                area.append("");
            }else{
                area.append(y);
            }
        }else{
            Toolkit.getDefaultToolkit().beep();
        }
    }    
}

public class MyOperatorListener implements ActionListener  {
    JTextArea area;
    double total;

    public MyOperatorListener(JTextArea area){
        this.area = area;
    }
    @Override
    public void actionPerformed(ActionEvent e) {

        JButton x = (JButton) e.getSource();
        String y = x.getText();
        double calc = Double.parseDouble(area.getText());

        switch(y){
            case "+": total = total + calc;
                break;
            case "-": total = total - calc;
                break;
            case "*": total = total * calc;
                break;
            case "/": if(calc!=0){
                      total = total / calc;
                      }
                break;
            default: break;
        }
        switch(y){
            case "CE": calc = 0;
                       area.setText("0");
                break;
            case "C": total = 0;
                      calc = 0;
                      area.setText("0");
                break;
            case "=": area.setText(Double.toString(total));
                break;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在您的操作侦听器中,仅在按下C时初始化总计。使用3 + 5的这个例子:

  • 按下3时,将数字输入该区域。
  • 按下+操作时,总计= 0 + 3 total = total + calc;
  • 输入5时,该数字将附加到该区域的末尾。 area.append(y);
  • 按下=操作时,该区域设置为总计,当前为3。

输入数字后,总数不会更新。

理想情况下,您希望输入和存储第一个数字(总计),然后在按下所需操作时,清除该区域以允许输入第二个数字。然后,equals操作通过调用要使用的操作来评估整个表达式。

  • 按3
  • 按+。总计存储在图3中,并且计算器记住正在使用+操作。然后清除该区域。
  • 按5。
  • 按=。然后计算器执行total + 5,因为它记得正在使用+操作。
相关问题