无法获取等于按钮以在Java Swing计算器中工作

时间:2018-10-30 02:20:02

标签: java swing actionlistener calculator

因此,单击功能按钮后,我的程序将当前数字(num1)存储在计算器上,然后存储选择的功能。当按下等号按钮时,应确定最后选择的功能是什么,并将其应用于先前的数字和当前数字(num2),并显示解决方案。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator extends JFrame implements ActionListener{
    JTextField input;
    JMenuBar menubar;
    JMenu file,help;
    JMenuItem exit;
    JButton clear,backspace,bDecimal,bDivide,bMult,bMinus,bPlus,bEquals,bNegative,bSqrt,bInverse;
    JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9;
    Double num1,num2 = null;
    JPanel numPanel,fcnPanel,outPanel;

    public Calculator(){
        //Make the frame
        this.setPreferredSize(new Dimension(909,909));
        this.setTitle("Vlad's Swing Calculator");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setLayout(new GridBagLayout());
        this.getContentPane().setBackground(Color.GREEN);

        //Create the menubar
        menubar = new JMenuBar();
        this.setJMenuBar(menubar);
        //menus
        file = new JMenu("File");
        menubar.add(file);
        help = new JMenu("Help");
        menubar.add(help);
        //menuItem
        exit = new JMenuItem("Exit");
        exit.addActionListener(this);
        file.add(exit);

        //Create text field
        input = new JTextField("",10);
        input.setBackground(Color.WHITE);
        input.setEditable(false);

        //create buttons
        clear = new JButton("Clear");
        backspace = new JButton("Backspace");
        b0 = new JButton(" 0 ");
        b1 = new JButton(" 1 ");
        b2 = new JButton(" 2 ");
        b3 = new JButton(" 3 ");
        b4 = new JButton(" 4 ");
        b5 = new JButton(" 5 ");
        b6 = new JButton(" 6 ");
        b7 = new JButton(" 7 ");
        b8 = new JButton(" 8 ");
        b9 = new JButton(" 9 ");
        bDecimal = new JButton(" .  ");
        bDivide = new JButton("/");
        bMult = new JButton("*");
        bMinus = new JButton("-");
        bPlus = new JButton("+");
        bEquals = new JButton("=");
        bNegative = new JButton("+/-");
        bSqrt = new JButton("sqrt");
        bInverse = new JButton("1/x");

        //add action listeners
        clear.addActionListener(this);
        backspace.addActionListener(this);
        b0.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        bDecimal.addActionListener(this);
        bDivide.addActionListener(this);
        bMult.addActionListener(this);
        bMinus.addActionListener(this);
        bPlus.addActionListener(this);
        bEquals.addActionListener(this);
        bNegative.addActionListener(this);
        bSqrt.addActionListener(this);
        bInverse.addActionListener(this);


        //Add buttons to panel
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(2,2,2,2);
        //add text field
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        this.add(input,gbc);

        //add numbers 0-9
        numPanel = new JPanel();
        numPanel.setLayout(new GridLayout(4,3));
        numPanel.add(b7);
        numPanel.add(b8);
        numPanel.add(b9);
        numPanel.add(b4);
        numPanel.add(b5);
        numPanel.add(b6);
        numPanel.add(b1);
        numPanel.add(b2);
        numPanel.add(b3);
        numPanel.add(b0);
        numPanel.add(bNegative);
        numPanel.add(bDecimal);


        // //Add function buttons
        fcnPanel = new JPanel();

        fcnPanel.setLayout(new GridLayout(4,2));
        fcnPanel.add(bSqrt);
        fcnPanel.add(bInverse);
        fcnPanel.add(bNegative);
        fcnPanel.add(bMinus);
        fcnPanel.add(bPlus);
        fcnPanel.add(bDivide);
        fcnPanel.add(bMult);
        fcnPanel.add(bEquals);

        //Add backspace and clear
        outPanel = new JPanel();
        outPanel.setLayout(new GridLayout(2,1));
        outPanel.add(clear);
        outPanel.add(backspace);

        this.add(outPanel);
        this.add(numPanel);
        this.add(fcnPanel);
        this.pack();

    }

    @Override
    public void actionPerformed(ActionEvent e){
        Object buttonPressed = e.getSource();
        Object lastButtonPressed = null;
        if(buttonPressed == exit){
            System.exit(0);
        }
        //Add action for clear
        if(buttonPressed == clear){
            input.setText("");
            num1 = num2 = null;
        }
        //Add action for backspace
        if(buttonPressed == backspace){
            String lastElementRemoved = input.getText();
            lastElementRemoved = lastElementRemoved.substring(0,lastElementRemoved.length()-1);
            input.setText(lastElementRemoved);
        }

        //Add event for each integer button
        if(buttonPressed == b0){
            //Make sure the starting zero will not be prepended
            if(input.getText() == "0")
            {
                input.setText("0");
            }
            else
                input.setText(input.getText() + "0");
        }
        if(buttonPressed == b1){
            if(input.getText() == "0"){
                input.setText("1");
            }
            else
                input.setText(input.getText() + "1");
        }
        if(buttonPressed == b2){
            //Make sure the starting zero will not be prepended
            if(input.getText() == "2")
            {
                input.setText("2");
            }
            else
                input.setText(input.getText() + "2");
        }
        if(buttonPressed == b3){
            //Make sure the starting zero will not be prepended
            if(input.getText() == "3")
            {
                input.setText("3");
            }
            else
                input.setText(input.getText() + "3");
        }
        if(buttonPressed == b4){
            //Make sure the starting zero will not be prepended
            if(input.getText() == "4")
            {
                input.setText("4");
            }
            else
                input.setText(input.getText() + "4");
        }
        if(buttonPressed == b5){
            //Make sure the starting zero will not be prepended
            if(input.getText() == "5")
            {
                input.setText("5");
            }
            else
                input.setText(input.getText() + "5");
        }
        if(buttonPressed == b6){
            //Make sure the starting zero will not be prepended
            if(input.getText() == "6")
            {
                input.setText("6");
            }
            else
                input.setText(input.getText() + "6");
        }
        if(buttonPressed == b7){
            //Make sure the starting zero will not be prepended
            if(input.getText() == "7")
            {
                input.setText("7");
            }
            else
                input.setText(input.getText() + "7");
        }
        if(buttonPressed == b8){
            //Make sure the starting zero will not be prepended
            if(input.getText() == "8")
            {
                input.setText("8");
            }
            else
                input.setText(input.getText() + "8");
        }
        if(buttonPressed == b9){
            //Make sure the starting zero will not be prepended
            if(input.getText() == "9")
            {
                input.setText("9");
            }
            else
                input.setText(input.getText() + "9");
        }

        //Add function for decimanl button
        if(buttonPressed == bDecimal){
            input.setText(input.getText() + ".");
        }
        //Add function for divide button
        if(buttonPressed == bDivide){
            lastButtonPressed = bDivide;
            num1 = Double.parseDouble(input.getText());
            input.setText("");
        }
        //Add Function for multiply button
        if(buttonPressed == bMult){
            lastButtonPressed = bMult;
            num1 = Double.parseDouble(input.getText());
            input.setText("");
        }
        //Add function to minus button
        if(buttonPressed == bMinus){
            lastButtonPressed = bMinus;
            num1 = Double.parseDouble(input.getText());
            input.setText("");
        }

        //Add function to plus button
        if(buttonPressed == bPlus){
            lastButtonPressed = bPlus;
            num1 = Double.parseDouble(input.getText());
            input.setText("");
        }
        //Add function for equals button
        if(buttonPressed == bEquals){
            //retrieve num2
            num2 = Double.parseDouble(input.getText());

            if(lastButtonPressed == bDivide){
                num1 = num1/num2;
                input.setText(num1.toString());
            }
            if(lastButtonPressed == bMult){
                num1 = num1*num2;
                input.setText(num1.toString());
            }
            if(lastButtonPressed == bMinus){
                num1 = num1-num2;
                input.setText(num1.toString());
            }
            if(lastButtonPressed == bPlus){
                num1 = num1+num2;
                input.setText(num1.toString());
            }
            else
                return;
        }

    }

    public static void main(String[] args){
        Calculator calc = new Calculator();
        calc.setVisible(true);
    }
}

我不认为equals部分中的代码正在执行,因为我放置在if(lastButtonPressed == bDivide)下的println无法打印。

因此,事实证明lastButtonPressed没有被更新。不知道为什么。

1 个答案:

答案 0 :(得分:0)

  

所以事实证明lastButtonPressed没有被更新

public void actionPerformed(ActionEvent e)
{
    Object buttonPressed = e.getSource();
    Object lastButtonPressed = null;

它正在更新,但是问题是您更新了值,然后在下次调用ActionListener时,将值重置为null。

因此,每次调用ActionListener时,您都会丢失有关上次按下的按钮的信息。

此外,请勿使用“ ==”来比较字符串。

if(input.getText() == "0"){

请改为使用equals(...)方法。

if ("0".equals(input.getText()) {

请注意,我也颠倒了顺序。万一input.getText()方法返回空对象,这将防止错误。