this is the code used to make the calculator work, I have not fully finished it yet. I'm am new to Java programming (at beginners level).
//listen to action from the buttons import java.awt.event.ActionListener; //implement event for button import java.awt.event.ActionEvent; import javax.swing.JButton; public class CalcFunction implements ActionListener{ /*reference to calculator using parent so the events for the button coded*/ Calculator parent; //used to store the math function char Selection=' '; //current result stored in the calculator is 0. double Current=0; //CalcFunction stores the reference to the to the Calculator class. CalcFunction(Calculator parent){ this.parent = parent; } @Override //implement the event for the buttons and the text field public void actionPerformed(ActionEvent e) { /*a gets the source of the button that was clicked in the calculator. Basically any button * that was clicked, the ActionListener will check which button is clicked and it will store * the clicked button's value. This will be stored in the new button created called * clickedButton*/ JButton clickedButton = (JButton) e.getSource(); /*the texts that are displayed in the calculator text field will be stored in the variable * named textfieldDisplay.*/ String textfieldDisplayed = parent.inPutText.getText(); //the value of the answers displayed will be displayed as 0.0 (in a a decimal value). double displayValue=0; if (!"".equals(textfieldDisplayed)){ // displayValue= Double.parseDouble(textfieldDisplayed); Object src = e.getSource(); if (src == parent.btnPlus){ Selection = '+'; Current=displayValue; parent.inPutText.setText(""); } else if (src == parent.btnMinus){ Selection = '-'; Current=displayValue; parent.inPutText.setText(""); } else if (src == parent.btnDivide){ Selection = '/'; Current=displayValue; parent.inPutText.setText(""); } else if (src == parent.btnMultiply){ Selection = '*'; Current=displayValue; parent.inPutText.setText(""); } if(Selection == '+'){ Selection='+'; Current+=displayValue; parent.inPutText.setText(""+ Current); //end bracket for parent.btnPlus } else if(Selection == '-'){ Selection='-'; Current-=displayValue; parent.inPutText.setText(""+ Current); //end bracket for parent.btnMinus } else if(Selection == '*'){ Selection='*'; Current*=displayValue; parent.inPutText.setText(""+ Current); //end bracket for parent.btnMultiply } else if(Selection == '/'){ Selection='/'; Current/=displayValue; parent.inPutText.setText("" + Current); //end bracket for parent.btnDivide } else{ // For all numeric buttons append the button's // label to the text field String ButtonLabel= clickedButton.getText(); parent.inPutText.setText(textfieldDisplayed + ButtonLabel); //end bracket for else. } } //end bracket for actionPerformed } }
below is the code used to set the calculator (how it looks like):
import java.awt.*; // import javax.swing.*; public class Calculator{ //sets the frame which is the window of the calculator. Where the buttons and the contents are //presented. Basically its the background box. This frame is assigned the name "Calculator". JFrame Frame = new JFrame("Calculator"); //sets the text box for the calculator. the maximum field size is 30. JTextField inPutText = new JTextField(30); //this JPanel is created to separately to add the buttons uniquely on its on section. //its almost like a graphical variable to store the buttons. JPanel buttonPanel = new JPanel(); //number buttons JButton btn1 = new JButton("1"); JButton btn2 = new JButton("2"); JButton btn3 = new JButton("3"); JButton btn4 = new JButton("4"); JButton btn5 = new JButton("5"); JButton btn6 = new JButton("6"); JButton btn7 = new JButton("7"); JButton btn8 = new JButton("8"); JButton btn9 = new JButton("9"); JButton btn0 = new JButton("0"); //math buttons JButton btnPlus = new JButton("+"); JButton btnMinus = new JButton("-"); JButton btnMultiply = new JButton("*"); JButton btnDivide = new JButton("/"); //full stop and equal button. JButton btnEqual = new JButton("="); JButton btnBulletPoint = new JButton("."); public Calculator(){ //sets a grid layout for the calculator 4 by 1. GridLayout buttonBorder = new GridLayout(4,1); //sets the frame visible to view the calculator itself. Frame.setVisible(true); //sets the frame size by 200 by 200. Frame.setSize(200,200); //sets the button panel visible to view the buttons. buttonPanel.setVisible(true); //the button panel is assigned with grid layout. buttonPanel.setLayout(buttonBorder); //added the buttons onto the Frame (Window) Frame.add(buttonPanel); //assigning the buttons to the Frame using the panel made for the buttons. buttonPanel.add(btn1); buttonPanel.add(btn2); buttonPanel.add(btn3); buttonPanel.add(btnPlus); buttonPanel.add(btn4); buttonPanel.add(btn5); buttonPanel.add(btn6); buttonPanel.add(btnMinus); buttonPanel.add(btn7); buttonPanel.add(btn8); buttonPanel.add(btn9); buttonPanel.add(btnMultiply); buttonPanel.add(btn0); buttonPanel.add(btn0); buttonPanel.add(btnBulletPoint); buttonPanel.add(btnEqual); buttonPanel.add(btnDivide); //assigning the text box on the Frame itself at the top of the box. Frame.add(inPutText,BorderLayout.NORTH); CalcFunction function= new CalcFunction(this); btn1.addActionListener(function); btn2.addActionListener(function); btn3.addActionListener(function); btn4.addActionListener(function); btn5.addActionListener(function); btn6.addActionListener(function); btn7.addActionListener(function); btn8.addActionListener(function); btn9.addActionListener(function); btn0.addActionListener(function); //public Calculator end bracket } public static void main(String[] args) { // Calculator calc = new Calculator(); //end of main } //end of class Calculator. }