JButton not performing it's Required Function in java -
here's code of currency converter program . earlier null pointer exception occurred , rectified . there no errors in program . program runs convert button not perform operation . believe problem lies in actionperformed function , i'm not being able identify .
import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.io.*; public class trial extends jframe implements actionlistener{ public string[] list={"dollars","euros ","yen","pounds","rupees"}; jpanel panel1 = new jpanel(); jpanel panel2 = new jpanel(); public jcombobox cb,cb1; public jtextfield tf1; public jtextfield tf2; public jlabel label3; public jlabel label4; jpanel panel3 = new jpanel(); public jbutton button1; public jbutton button2; public trial(){ setlayout(new boxlayout(getcontentpane(),boxlayout.y_axis)); getcontentpane().setlayout( new boxlayout(getcontentpane(), boxlayout.page_axis) ); jlabel label1 = new jlabel("from :"); label1.setfont(new font("serif", font.bold, 15)); jcombobox cb = new jcombobox(); cb= new jcombobox(list); cb.getselectedindex(); panel1.setbackground(color.red); panel1.add(label1); panel1.add(cb); jlabel label2 = new jlabel("to :"); label2.setfont(new font("serif", font.bold, 15)); jcombobox cb1 = new jcombobox(); cb1=new jcombobox(list); cb1.getselectedindex(); cb.addactionlistener(this); cb1.addactionlistener(this); panel1.add(label2); panel1.add(cb1); add(panel1); jlabel label3 = new jlabel("\n\nenter amount :"); label3.setfont(new font("serif", font.bold, 15)); jtextfield tf1 = new jtextfield(15); // label3.sethorizontalalignment(swingconstants.center); // label3.setverticalalignment(swingconstants.center); panel2.add(label3); panel2.add(tf1); jlabel label4 = new jlabel("converted amount :"); label4.setfont(new font("serif", font.bold, 15)); label4.setdisplayedmnemonic(keyevent.vk_o); jtextfield tf2 = new jtextfield(15); tf1.addactionlistener(this); tf2.addactionlistener(this); panel2.add(label4); panel2.add(tf2); panel2.setbackground(color.blue); add(panel2); jbutton button1 = new jbutton("convert"); button1.setfont(new font("serif", font.bold, 15)); button1.setmnemonic(keyevent.vk_k); panel3.add(button1); jbutton button2 = new jbutton("clear "); button2.setfont(new font("serif", font.bold, 15)); button2.setmnemonic(keyevent.vk_c); button1.addactionlistener(this); button2.addactionlistener(this); panel3.add(button2); panel3.setbackground(color.green); add(panel3); } public void actionperformed(actionevent e) { double a,b=0; object src = e.getsource(); if(src.equals(button1)){ a=double.valueof(tf1.gettext()); try{ if(cb.getselectedindex()==0 && cb1.getselectedindex()==1) {b=a*0.89;} if(cb.getselectedindex()==0 && cb1.getselectedindex()==2) {b=a*124.75;} if(cb.getselectedindex()==0 && cb1.getselectedindex()==3) {b=a*0.65;} if(cb.getselectedindex()==0 && cb1.getselectedindex()==4) {b=a*64.08;} if(cb.getselectedindex()==1 && cb1.getselectedindex()==0) {b=a*1.13;} if(cb.getselectedindex()==1 && cb1.getselectedindex()==2) {b=a*140.49;} if(cb.getselectedindex()==1 && cb1.getselectedindex()==3) {b=a*0.74;} if(cb.getselectedindex()==1 && cb1.getselectedindex()==4) {b=a*71.34;} if(cb.getselectedindex()==2 && cb1.getselectedindex()==0) {b=a*0.0080;} if(cb.getselectedindex()==2 && cb1.getselectedindex()==1) {b=a*0.0071;} if(cb.getselectedindex()==2 && cb1.getselectedindex()==3) {b=a*0.0052;} if(cb.getselectedindex()==2 && cb1.getselectedindex()==4) {b=a*0.51;} if(cb.getselectedindex()==3 && cb1.getselectedindex()==0) {b=a*1.53;} if(cb.getselectedindex()==3 && cb1.getselectedindex()==1) {b=a*1.36;} if(cb.getselectedindex()==3 && cb1.getselectedindex()==2) {b=a*191.26;} if(cb.getselectedindex()==3 && cb1.getselectedindex()==4) {b=a*97.88;} if(cb.getselectedindex()==4 && cb1.getselectedindex()==0) {b=a*0.0156;} if(cb.getselectedindex()==4 && cb1.getselectedindex()==1) { b=a*0.014;} if(cb.getselectedindex()==4 && cb1.getselectedindex()==2) {b=a*1.9607;} if(cb.getselectedindex()==4 && cb1.getselectedindex()==3) {b=a*0.0108;} tf2.settext(string.valueof(b)); }catch(exception x){system.out.println("error");} } if(src.equals(button2)){ tf1.settext("0000"); tf2.settext("0000"); } } }
you're shadowing variable button1
. replace
jbutton button1 = new jbutton("convert");
with
button1 = new jbutton("convert");
Comments
Post a Comment