1

I have been trying to make a calculator in Javax swing. I know how to make one button have one command, but I don't know how to give more buttons more commands in Java? I want to give buttons a0-clear actions.

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

public class Calculator extends JFrame {

    private JButton a0;
    private JButton a1;
    private JButton a2;
    private JButton a3;
    private JButton a4;
    private JButton a5;
    private JButton a6;
    private JButton a7;
    private JButton a8;
    private JButton a9;
    private JButton clear;
    private JButton plusminus;
    private JButton plus;
    private JButton minus;
    private JButton multiply;
    private JButton divide;
    private JButton equal;
    private JButton decimal;

    private JLabel resultLabel;

    public Calculator() {
        setLayout(new FlowLayout());

        a0 = new JButton("0");
        add(a0);

        a1 = new JButton("1");
        add(a1);

        a2 = new JButton("2");
        add(a2);

        a3 = new JButton("3");
        add(a3);

        a4 = new JButton("4");
        add(a4);

        a5 = new JButton("5");
        add(a5);

        a6 = new JButton("6");
        add(a6);

        a7 = new JButton("7");
        add(a7);

        a8 = new JButton("8");
        add(a8);

        a9 = new JButton("9");
        add(a9);

        decimal = new JButton(".");
        add(decimal);

        clear = new JButton("C");
        add(clear);

        plusminus = new JButton("+/-");
        add(plusminus);

        plus = new JButton("+");
        add(plus);

        minus = new JButton("-");
        add(minus);

        multiply = new JButton("X");
        add(multiply);

        divide = new JButton("/");
        add(divide);

        equal = new JButton("=");
        add(equal);

        resultLabel = new JLabel("");
        add(resultLabel);

    }

    public class event implements ActionListener {

        public void actionPerformed(ActionEvent event) {

            a0.addActionListener(this);

            a1.addActionListener(this);

            a2.addActionListener(this);

            a3.addActionListener(this);

            a4.addActionListener(this);

            a5.addActionListener(this);

            a6.addActionListener(this);

            a7.addActionListener(this);

            a8.addActionListener(this);

            a9.addActionListener(this);

            clear.addActionListener(this);

            decimal.addActionListener(this);

            plusminus.addActionListener(this);

            plus.addActionListener(this);

            minus.addActionListener(this);

            multiply.addActionListener(this);

            divide.addActionListener(this);

            equal.addActionListener(this);

            if (event.getSource() == a1) {
                resultLabel.setText("0");
            }

        }
    }

    public static void main(String[] args) {
        Calculator gui = new Calculator();
        gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gui.setSize(300, 800);
        gui.setVisible(true);
        gui.setTitle("Caclculator");

    }
}
3
  • Probably you need a different action listener per button, or have the action listener check the button firing the event. Commented Dec 1, 2015 at 20:42
  • You're adding the ActionListener to your buttons in the actionPerformed method. You should probably do this in your constructor instead. For your question, you're on the right track -- You need to determine which button was pressed with conditional statements: if (event.getSource() == a1) Commented Dec 1, 2015 at 20:49
  • Have a look at How to Use Actions Commented Dec 1, 2015 at 23:26

2 Answers 2

1

I am going to show you for 1-button, do the same for all other,

public class Calculator extends JFrame  implements ActionListener {

    JButton a0 = new JButton("0");
    a0.addActionListener(this);
    add(a0); 

    public void actionPerformed(ActionEvent event) {

        String command = e.getActionCommand();

        switch(command){

            case "0":
                    //do whatever want while button '0' press
                    break;
            case "1":
                    //do whatever want while button '1' press
                    break;
            case "2":
                    //do whatever want while button '2' press
                    break;      


        }

    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You will create a class which implements ActionListener.

Then you create and instance of this class and then add this as ActionListener to every button(Code Below):

@SuppressWarnings("serial")
public class Calculator extends JFrame {

// array with the number 0-9
private JButton[] numbers = new JButton[9];
private JButton clear;
private JButton plusminus;
private JButton plus;
private JButton minus;
private JButton multiply;
private JButton divide;
private JButton equal;
private JButton decimal;

private JLabel resultLabel = new JLabel("");

public Calculator() {

    //
    setSize(300, 300);
    setLayout(new FlowLayout());

    // The Class which implements the ActionListener
    EventListener listener = new EventListener();

    // Create each button from 0-9 here
    for (int i = 0; i < numbers.length; i++) {
        numbers[i] = new JButton(i + "");
        numbers[i].addActionListener(listener);
        add(numbers[i]);
    }

    // Create the other Buttons
    decimal = new JButton(".");
    add(decimal);

    clear = new JButton("C");
    add(clear);

    plusminus = new JButton("+/-");
    add(plusminus);

    plus = new JButton("+");
    add(plus);

    minus = new JButton("-");
    add(minus);

    multiply = new JButton("X");
    add(multiply);

    divide = new JButton("/");
    add(divide);

    equal = new JButton("=");
    add(equal);

    clear.addActionListener(listener);

    decimal.addActionListener(listener);

    plusminus.addActionListener(listener);

    plus.addActionListener(listener);

    minus.addActionListener(listener);

    multiply.addActionListener(listener);

    divide.addActionListener(listener);

    equal.addActionListener(listener);

    add(resultLabel);
}

public static void main(String[] args) {
    Calculator gui = new Calculator();
    gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    gui.setSize(300, 800);
    gui.setVisible(true);
    gui.setTitle("Caclculator");

}

// TODO EventListener
 class EventListener implements ActionListener {

    public void actionPerformed(ActionEvent event) {
        Object v = event.getSource();

        if (v == clear) {
            // do something..
        } else if (v == decimal) {
            // do something...
        } else if (v == plusminus) {
            // do something...
        }
        // etc continue this.....

    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.