2

I am new to java swing so decided to create a basic calculator I have used two listeners which in fact are interfaces the first interface is used to listens to digit(0-9) and the second is used to listen to the operators(-,+,/);

The problem I have is that I used two different interfaces to listen to the buttons so this means the operator buttons are not working;

First Interface(digitListener)

package calculator;

public interface DigitListener {
    public void StringEmmiter(String text1);
}

Second Interface(OperatorListener) package calculator;

public interface MathOperatorListener {
    public void OperatorEmitter(String text2);

}

ButtonTemplate is a class which set up how the button looks this is used to stop my code in FormPanel from being really long

package calculator;

import java.awt.Dimension;

import javax.swing.JButton;

public class ButtonTemplate extends JButton{
    private String nameOfTheButton;

    public ButtonTemplate(String nameOfButton){
        this.nameOfTheButton = nameOfButton;
        this.setText(nameOfTheButton);
        this.setPreferredSize(new Dimension(70,50));

    }


}

Formpanel is the actual calculator //problem here

package calculator;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FormArea extends JPanel implements ActionListener{
    private ButtonTemplate No1;
    private ButtonTemplate No2;
    private ButtonTemplate No3;
    private ButtonTemplate No4;
    private ButtonTemplate No5;
    private ButtonTemplate No6;
    private ButtonTemplate No7;
    private ButtonTemplate No8;
    private ButtonTemplate No9;
    private ButtonTemplate No0;
    private ButtonTemplate addition;
    private ButtonTemplate substraction;
    private ButtonTemplate division;
    private ButtonTemplate total;
    private ButtonTemplate multiplication;
    private JButton clearButton;

    private DigitListener digitListener;
    private MathOperatorListener operatorListener;

    public FormArea(){
        //SetSize
        setPreferredSize(new Dimension(300,500));
        //Create all Objects
        No1 = new ButtonTemplate("1");
        No2 = new ButtonTemplate("2");
        No3 = new ButtonTemplate("3");
        No4 = new ButtonTemplate("4");
        No5 = new ButtonTemplate("5");
        No6 = new ButtonTemplate("6");
        No7 = new ButtonTemplate("7");
        No8 = new ButtonTemplate("8");
        No9 = new ButtonTemplate("9");
        No0 = new ButtonTemplate("0");
        addition = new ButtonTemplate("+");
        substraction = new ButtonTemplate("-");
        division = new ButtonTemplate("/");
        total = new ButtonTemplate("=");
        multiplication = new ButtonTemplate("x");
        clearButton = new JButton("CLEAR");

        clearButton.setPreferredSize(new Dimension(100,60));

        setLayout(new GridBagLayout());

        GridBagConstraints gc = new GridBagConstraints();

        //Layout

        //Column 1
        gc.gridx = 0;
        gc.gridy = 0;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(No1,gc);

        gc.gridx = 0;
        gc.gridy = 1;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(No4,gc);

        gc.gridx = 0;
        gc.gridy = 2;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(No7,gc);

        //Column 2
        gc.gridx = 1;
        gc.gridy = 0;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(No2,gc);

        gc.gridx = 1;
        gc.gridy = 1;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(No5,gc);

        gc.gridx = 1;
        gc.gridy = 2;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(No8,gc);

        //Column3
        gc.gridx = 2;
        gc.gridy = 0;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(No3,gc);

        gc.gridx = 2;
        gc.gridy = 1;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(No6,gc);

        gc.gridx = 2;
        gc.gridy = 2;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(No9,gc);

        //Operators

        gc.gridx = 0;
        gc.gridy = 3;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(addition,gc);

        gc.gridx = 1;
        gc.gridy = 3;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(No0,gc);

        gc.gridx = 2;
        gc.gridy = 3;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(substraction,gc);

        //Operator 2

        gc.gridx = 0;
        gc.gridy = 4;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(division,gc);

        gc.gridx = 1;
        gc.gridy = 4;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(multiplication,gc);

        gc.gridx = 2;
        gc.gridy = 4;
        gc.weightx = 0.1;
        gc.weighty = 0.1;
        add(total,gc);


        gc.gridx = 1;
        gc.gridy = 5;
        gc.weightx = 0.1;
        gc.weighty = 5;
        add(clearButton,gc);

        //ActionButton
        No1.addActionListener(this);
        No2.addActionListener(this);
        No3.addActionListener(this);
        No4.addActionListener(this); 
        No5.addActionListener(this); 
        No6.addActionListener(this); 
        No7.addActionListener(this);
        No8.addActionListener(this); 
        No9.addActionListener(this); 
        No0.addActionListener(this);
        addition.addActionListener(this); 
        substraction.addActionListener(this);
        division.addActionListener(this); 
        total.addActionListener(this); 
        multiplication.addActionListener(this);
        clearButton.addActionListener(this);

    }

    public void setStringListener(DigitListener digitlistener){
        this.digitListener = digitlistener;
    }

    public void setOperatorListener(MathOperatorListener operatorListener){
        this.operatorListener = operatorListener;
    }


    public ButtonTemplate[] iterateThroughButton(){
        ButtonTemplate btn[] = {No1,No2,No3,No4,No5,No6,No7,No8,No9,No0};
        return btn;
    }

    public ButtonTemplate[] iterateThroughOperators(){
        ButtonTemplate operatorBtn[] = {addition,substraction,division,
                total,multiplication,(ButtonTemplate)clearButton};

        return operatorBtn;
    }

    public void actionPerformed(ActionEvent e) {
        JButton source = (JButton)e.getSource();

        for(ButtonTemplate SingleButton : iterateThroughButton()){
            if(source == SingleButton){

                digitListener.StringEmmiter(SingleButton.getText());

            }
        }
            for(ButtonTemplate OperatorSingleButton : iterateThroughOperators()){
                if(source == OperatorSingleButton){
                    System.out.println("Hello");
                    operatorListener.OperatorEmitter(OperatorSingleButton.getText());

                }
            }
        }

    }

MainFrame is the class which create objects

package calculator;

import java.awt.BorderLayout;

import javax.swing.JFrame;

public class MainFrame extends JFrame{

    private FormArea formpanel;
    private OutputArea outputText;

    public MainFrame(){
        super("Calculator");
        setVisible(true);
        setSize(800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        formpanel = new FormArea();
        outputText = new OutputArea();



        setLayout(new BorderLayout());

        formpanel.setStringListener(new DigitListener() {

            public void StringEmmiter(String text) {
                outputText.addText(text);
            }
        });

        formpanel.setOperatorListener(new MathOperatorListener() {

            public void OperatorEmitter(String text2) {
                outputText.addText(text2);

            }
        });

        add(formpanel,BorderLayout.WEST);
        add(outputText, BorderLayout.CENTER);


    }

}

Thanks for looking at my code much appreciated!

dijam
  • 658
  • 1
  • 9
  • 21
  • 3
    1) If there is a question among those words, please add a `?` to the end of it. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Aug 23 '15 at 07:31
  • 1
    FYI, use arrays. It will shorten your code a lot. – Spikatrix Aug 23 '15 at 07:31
  • @CoolGuy I have used arrays to check the which button has been selected please provide an example? – dijam Aug 23 '15 at 08:04
  • @AndrewThompson Thanks for the example! – dijam Aug 23 '15 at 08:08

0 Answers0