java swing, get value from ActionListener -
i wanted click on button , have pop window can enter string , string outputted in label, can't manage return string.
jbutton btnname = new jbutton("name"); btnname.addactionlistener(new actionlistener() { string name; public void actionperformed(actionevent e) { name = joptionpane.showinputdialog("enter name"); } }); btnname.setbounds(10, 11, 89, 23); frame.getcontentpane().add(btnname); jlabel lblperson = new jlabel(name); lblperson.setfont(new font("tahoma", font.plain, 36)); lblperson.setbounds(10, 188, 414, 63); frame.getcontentpane().add(lblperson);`
i don't know how return string name actionlistener class have error @ line 10.
just use jlabel.settext
public void actionperformed(actionevent e) { name = joptionpane.showinputdialog("enter name"); lblperson.settext(name); }
another way can creating (abstract)action inner classes in class, using jbutton.setaction(myaction);
Comments
Post a Comment