-4

The GUI of my code is perfect, but the calculator is not working, i.e. when I click on one button it is not getting displayed in the text filed. This is my code:-

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

class calc extends Frame implements ActionListener
{
int resulta=0;
int resultb=1;
JTextField tf;
int result;
private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b16;

calc()
{
tf=new JTextField();
tf.setBounds(60,50,200,20);
JButton b1=new JButton("0");
b1.setBounds(60,100,30,30);

JButton b2=new JButton("1");
b2.setBounds(100,100,30,30);

JButton b3=new JButton("2");
b3.setBounds(140,100,30,30);

JButton b4=new JButton("3");
b4.setBounds(60,150,30,30);

JButton b5=new JButton("4");
b5.setBounds(100,150,30,30);

JButton b6=new JButton("5");
b6.setBounds(140,150,30,30);

JButton b7=new JButton("6");
b7.setBounds(60,200,30,30);

JButton b8=new JButton("7");
b8.setBounds(100,200,30,30);

JButton b9=new JButton("8");
b9.setBounds(140,200,30,30);

JButton b10=new JButton("9");
b10.setBounds(60,250,30,30);

JButton b11=new JButton("+");
b11.setBounds(100,250,30,30);

JButton b12=new JButton("-");
b12.setBounds(140,250,30,30);

JButton b13=new JButton("/");
b13.setBounds(60,300,30,30);

JButton b14=new JButton("*");
b14.setBounds(100,300,30,30);


JButton b16=new JButton("Result");
b16.setBounds(60,350,100,20);


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);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);

b16.addActionListener(this);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);

add(b16);
add(tf);
setSize(500,500);
setLayout(null);
setVisible(true);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
tf.setText(b1.getText());
else if(e.getSource()==b2)
tf.setText(b2.getText());
else if(e.getSource()==b3)
tf.setText(b3.getText());
else if(e.getSource()==b4)
tf.setText(b4.getText());
else if(e.getSource()==b5)
tf.setText(b5.getText());
else if(e.getSource()==b6)
tf.setText(b6.getText());
else if(e.getSource()==b7)
tf.setText(b7.getText());
else if(e.getSource()==b8)
tf.setText(b8.getText());
else if(e.getSource()==b9)
tf.setText(b9.getText());
else if(e.getSource()==b10)
tf.setText(b10.getText());
String v1=tf.getText();
int num1= Integer.parseInt(v1);

if(e.getSource()==b10||e.getSource()==b11||e.getSource()==b12||e.getSource()==b13||e.getSource()==b14)
{
tf.setText("");


Object op= e.getSource();

if(op == b11)
{
resulta=resulta+num1;
}
else if(op == b12)
{
resulta=num1-resulta;
}
else if(op == b13)
{
resultb=num1/resultb;
}
else if(op == b14)
{
resultb=num1*resultb;
}

if(op==b11||op==b12)
result=resulta;
else 
result=resultb;
if(op==b16)
JOptionPane.showMessageDialog(this,"Result"+result);
}
}
public static void main(String args[])
{
new calc();
}
}

I cannot type the error here as it is tooo long.

  • try formatting the code above, it makes it easier for others to help you. Also the "too long error" is due to `java.lang.NumberFormatException`, specifically you are trying to convert `""` to an `Integer` – Tamer Jul 06 '14 at 16:19
  • You are trying to compare Objects using `==` this wont work, use `.equals(...)` instead. This might help: http://stackoverflow.com/questions/7520432/java-vs-equals-confusion – maximus_de Jul 06 '14 at 16:27
  • @maximus_de That's not the problem. Actually, `getSource` returns the reference of the clicked object, so `==` is the correct way to do the comparison. – Hugo Sousa Jul 06 '14 at 16:29
  • @HugoSousa well, I used `.equals(...)` all the time in such case and never `==`. Moreover `==` didnt work... but I dont deny that I mifht be wrong. – maximus_de Jul 06 '14 at 16:50

1 Answers1

1

The problem is that you're redifining the JButton variables in the constructor.

So, initialize your JButtonslike this:

b1=new JButton("0");

instead of

JButton b1=new JButton("0");

Otherwise, the getSource in the actionPerformed method won't work properly, it won't enter in any if condition, because the buttons aren't the originally declared.

Hugo Sousa
  • 1,904
  • 2
  • 15
  • 28