java - The GUI is perfect, but the calculator code does not work -
the gui of code perfect, calculator not working, i.e. when click on 1 button not getting displayed in text filed. 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 error here tooo long.
the problem you're redifining jbutton
variables in constructor.
so, initialize jbuttons
like this:
b1=new jbutton("0");
instead of
jbutton b1=new jbutton("0");
otherwise, getsource
in actionperformed
method won't work properly, won't enter in if
condition, because buttons aren't declared.
Comments
Post a Comment