here is the code I wrote:
import javax.swing.*; import java.awt.MenuBar; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class NewCalculator implements ActionListener { //assign button clicked number and answer onto variables. int numone=0,numtwo=0, answer=0; char operator=' '; //components of calculator JButton one = new JButton("1"); JButton two = new JButton("2"); JButton three = new JButton("3"); JButton four = new JButton("4"); JButton five = new JButton("5"); JButton six= new JButton("6"); JButton seven = new JButton("7"); JButton eight= new JButton("8"); JButton nine = new JButton("9"); JButton zero = new JButton("0"); JButton C= new JButton("C"); JButton CE = new JButton("CE"); JButton plus = new JButton("+"); JButton minus= new JButton("-"); JButton multiply = new JButton("*"); JButton divide = new JButton("/"); JButton sqrt = new JButton("sqrt"); JButton percent = new JButton("%"); JButton equal = new JButton("="); JButton x = new JButton("1/x"); JButton plusmin = new JButton("+/-"); JButton fullstop = new JButton("."); JButton backsp = new JButton("backspace"); JTextField textfl=new JTextField(); JMenuBar menu = new JMenuBar(); //used to implement panels for calculator layout. JPanel NewCalculator(){ //action listener to listen to event on these contents. one.addActionListener(this); two.addActionListener(this); three.addActionListener(this); four.addActionListener(this); five.addActionListener(this); six.addActionListener(this); seven.addActionListener(this); eight.addActionListener(this); nine.addActionListener(this); zero.addActionListener(this); plus.addActionListener(this); minus.addActionListener(this); multiply.addActionListener(this); divide.addActionListener(this); sqrt.addActionListener(this); percent.addActionListener(this); equal.addActionListener(this); x.addActionListener(this); plusmin.addActionListener(this); fullstop.addActionListener(this); backsp.addActionListener(this); textfl.addActionListener(this); //main panel JPanel parent = new JPanel(); //manually modify panels parent.setLayout(null); //panel for number buttons JPanel numbers = new JPanel(); numbers.setSize(150,150); numbers.setLocation(10,70); numbers.add(one); numbers.add(two); numbers.add(three); numbers.add(four); numbers.add(five); numbers.add(six); numbers.add(seven); numbers.add(eight); numbers.add(nine); numbers.add(zero); numbers.add(equal); numbers.add(zero); numbers.add(fullstop); numbers.add(plusmin); //add content to main parent panel parent.add(numbers); //panel for math button JPanel math = new JPanel(); math.setSize(100,190); math.setLocation(160,50); math.add(C); math.add(CE); math.add(plus); math.add(minus); math.add(multiply); math.add(divide); math.add(equal); math.add(sqrt); math.add(percent); //add content to main parent panel parent.add(math); //calling new backspace button to position it separately on the parent panel backsp=new JButton("Backspace"); backsp.setSize(120,20); backsp.setLocation(20,50); parent.add(backsp); //calling new text field to position it separately on the parent panel textfl=new JTextField(); textfl.setSize(200,20); textfl.setLocation(20,20); //add content to main parent panel parent.add(textfl); //calling menu separately to position it on the parent panel menu = new JMenuBar(); menu.setSize(370,20); menu.setLocation(0,0); JMenu help = new JMenu("help"); JMenu about = new JMenu("about"); menu.add(help); menu.add(about); //add content to main parent panel parent.add(menu); //returns the main parent panel with all the sub panels and contents return parent; } //creating frame separately private static void frameCreation(){ JFrame title = new JFrame("calculator"); NewCalculator calcul = new NewCalculator(); title.setContentPane(calcul.NewCalculator()); title.setSize(370, 320); title.setVisible(true); } public static void main(String[] args){ //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { frameCreation(); } }); //end of main } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub //identifies the actions of each separate components even if all the component actions //are the same String action = e.getActionCommand(); if(action.equals("=")){ //gets the text in text filed and convert it to integer value to be used to calculate. numtwo=Integer.parseInt(textfl.getText()); //deletes the old text displayed on the text filed to display the answer (eclipse). textfl.setText(""); Answer(); DisplayResults(); } //if the C button is clicked else if(action.equals("C")){ //clears the text on the text field. textfl.setText(""); } else if(action.equals("CE")){ //clears the text on the text field. textfl.setText(""); } else if(action.equals("+")){ operator='+'; numone=Integer.parseInt(textfl.getText()); operator=e.getActionCommand().charAt(0); textfl.setText(""); } else if(action.equals("-")){ operator='-'; numone=Integer.parseInt(textfl.getText()); operator=e.getActionCommand().charAt(0); textfl.setText(""); } else if(action.equals("*")){ operator='*'; numone=Integer.parseInt(textfl.getText()); operator=e.getActionCommand().charAt(0); textfl.setText(""); } else if(action.equals("/")){ operator='/'; numone=Integer.parseInt(textfl.getText()); operator=e.getActionCommand().charAt(0); textfl.setText(""); } else if(action.equals("%")){ operator='/'; numone=Integer.parseInt(textfl.getText()); operator=e.getActionCommand().charAt(0); textfl.setText(""); } else { textfl.setText(textfl.getText()+action); } //actionPerformed end } //if one of the math buttons are clicked then one of the mathematical calculation will be //calculated. public void Answer(){ switch(operator) { case '+': answer=numone+numtwo; break; case '-': answer=numone-numtwo; break; case '*': answer=numone*numtwo; return; case '/': //if user attempts to divide by 0, then an error message is displayed. if(numtwo==0){ textfl.setText("cannot divide by 0"); textfl.setText(""); } else{ //calculates answer answer=numone/numtwo; } break; default: answer=0; } } //displays the answer. public void DisplayResults(){ textfl.setText(String.valueOf(answer)); } } //public class end
Also what is the code I need to use to do the percentage and square root calculation in the calculation. Cos I am not sure about the sign I am required to use to do the calculation.
thanks in advance.
--- Update ---
the if statement is where the code to make the clear button (C and CE) work.