重新添加/重新划分不起作用

时间:2017-08-25 19:13:19

标签: java swing jframe

我从网上下载了一个计算器程序,然后根据需要自定义它。编辑代码后,当我添加假设2 + 3时,它将显示正确的答案。但在那之后,如果我分开/相乘或做任何事情,它就没有表现出正确的答案。

equalsButton.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent event)
        {
            if (!numberCalc.getText().isEmpty())
            {
                int number = Integer.parseInt(numberCalc.getText());
                String calc;
                double calculate;

                switch (calcOperation){

                    case 1:
                    calculate = currentCalc  + number;
                    currentCalc = calculate;
                    calc = String.valueOf(calculate);
                    numberCalc.setText(calc);
                    break;

                    case 2:
                    calculate = currentCalc  - number;
                    currentCalc = calculate;
                    calc = String.valueOf(calculate);
                    numberCalc.setText(calc);
                    break;

                    case 3:
                    calculate = currentCalc * number;
                    currentCalc = calculate;
                    calc = String.valueOf(calculate);
                    numberCalc.setText(calc);
                    break;

                    case 4:
                    calculate = currentCalc / number;
                    currentCalc = calculate;
                    if(currentCalc == 0){
                        JOptionPane.showMessageDialog(null, "You Can't divide 0! Have a good day!");
                    }
                    calc = String.valueOf(calculate);
                    numberCalc.setText(calc);
                    break;

                    case 5:
                    numberCalc.setText(null);
                    break;
                }

            }
          }
        });

1 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些更改。这应该工作。它抛出一个异常,因为当你使用前一个操作的double类型输出进入下一个操作时,使用Integer.parseInt(..)解析一个double值。

package com.example;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;

import javax.imageio.ImageIO;
import javax.swing.*;


    public class SimpleCalc implements ActionListener{

        JFrame guiFrame;
        JPanel buttonPanel;
        JTextField numberCalc;
        int calcOperation = 0;
        double currentCalc;

        //Note: Typically the main method will be in a
        //separate class. As this is a simple one class
        //example it's all in the one class.
        public static void main(String[] args) {

             //Use the event dispatch thread for Swing components
             EventQueue.invokeLater(new Runnable()
             {

                @Override
                 public void run()
                 {

                     new SimpleCalc();         
                 }
             });

        }

        public SimpleCalc()
        {
            guiFrame = new JFrame();

            //make sure the program exits when the frame closes
            guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            guiFrame.setTitle("Calculator Project");
            guiFrame.setSize(400,400);

            //This will center the JFrame in the middle of the screen
            guiFrame.setLocationRelativeTo(null);

            numberCalc = new JTextField();
            numberCalc.setHorizontalAlignment(JTextField.RIGHT);
            numberCalc.setLayout(null);
            numberCalc.setSize(100,400);
            numberCalc.setEditable(false);

            guiFrame.add(numberCalc, BorderLayout.NORTH);

            buttonPanel = new JPanel();


            buttonPanel.setLayout(new GridLayout(4,4));   
            guiFrame.add(buttonPanel, BorderLayout.CENTER);

            //Add the number buttons
            for (int i=0;i<10;i++)
            {
                addButton(buttonPanel, String.valueOf(i));
            }

            JButton clrButton = new JButton("C");
            clrButton.setActionCommand("C");

            OperatorAction clrAction = new OperatorAction(5);
            clrButton.addActionListener(clrAction);

            JButton subsButton = new JButton("*");
            subsButton.setActionCommand("*");

            OperatorAction subsAction = new OperatorAction(3);
            subsButton.addActionListener(subsAction);

            JButton divideButton = new JButton("/");
            divideButton.setActionCommand("/");

            OperatorAction divideAction = new OperatorAction(4);
            divideButton.addActionListener(divideAction);


            JButton addButton = new JButton("+");
            addButton.setActionCommand("+");

            OperatorAction subAction = new OperatorAction(1);
            addButton.addActionListener(subAction);

            JButton subButton = new JButton("-");
            subButton.setActionCommand("-");

            OperatorAction addAction = new OperatorAction(2);
            subButton.addActionListener(addAction);

            JButton equalsButton = new JButton("=");
            equalsButton.setActionCommand("=");
            equalsButton.addActionListener(new ActionListener()
            {

                public void actionPerformed(ActionEvent event)
                {
                    if (!numberCalc.getText().isEmpty())
                    {
                        int number = Integer.parseInt(numberCalc.getText());
                        String calc;
                        double calculate;

                        switch (calcOperation){

                            case 1:
                            calculate = currentCalc  + number;
                            currentCalc = calculate;
                            calc = String.valueOf(calculate);
                            numberCalc.setText(calc);
                            break;

                            case 2:
                            calculate = currentCalc  - number;
                            currentCalc = calculate;
                            calc = String.valueOf(calculate);
                            numberCalc.setText(calc);
                            break;

                            case 3:
                            calculate = currentCalc * number;
                            currentCalc = calculate;
                            calc = String.valueOf(calculate);
                            numberCalc.setText(calc);
                            break;

                            case 4:
                            calculate = currentCalc / number;
                            currentCalc = calculate;
                            if(currentCalc == 0){
                                JOptionPane.showMessageDialog(null, "You Can't  divide 0! Have a good day!");
                            }
                            calc = String.valueOf(calculate);
                            numberCalc.setText(calc);
                            break;

                            case 5:
                            numberCalc.setText(null);
                            break;
                        }

                    }




                    }
                });

            buttonPanel.add(addButton);
            buttonPanel.add(subButton);
            buttonPanel.add(subsButton);
            buttonPanel.add(divideButton);
            buttonPanel.add(equalsButton);
            buttonPanel.add(clrButton);
            guiFrame.setVisible(true);  
        }

        //All the buttons are following the same pattern
        //so create them all in one place.
        private void addButton(Container parent, String name)
        {
            JButton but = new JButton(name);
            but.setActionCommand(name);
            but.addActionListener(this);
            parent.add(but);
        }


        @Override
        public void actionPerformed(ActionEvent event)
        {
            //get the Action Command text from the button
            String action = event.getActionCommand();

            //set the text using the Action Command text
            numberCalc.setText(action);       
        }



        private class OperatorAction implements ActionListener
        {
            private int operator;

            public OperatorAction(int operation)
            {
                operator = operation;
            }

            public void actionPerformed(ActionEvent event)
            {
                currentCalc = Double.parseDouble(numberCalc.getText()); 
                calcOperation = operator;
            }
        }
    }