swing - java radio button, updating a JLabel -
i trying make code adds or subtracts initial value presented each time click submit. have searched multiple threads on how complete , haven't come across clear. push in right direction help.
package twopaneldesign; import javax.swing.jframe; import java.awt.color; import java.awt.font; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.buttongroup; import javax.swing.jpanel; import javax.swing.jradiobutton; import javax.swing.jlabel; import javax.swing.swingconstants; import javax.swing.jbutton; public class twopaneldesign extends jframe { private static final long serialversionuid = 1l; public twopaneldesign() { getcontentpane().setlayout(null); jpanel panel = new jpanel(); panel.setbounds(0, 0, 151, 262); getcontentpane().add(panel); panel.setlayout(null); final jlabel lblyourchoice = new jlabel("your choice..."); lblyourchoice.setbounds(10, 11, 172, 14); panel.add(lblyourchoice); final jradiobutton rdbtnadd = new jradiobutton("add 2"); rdbtnadd.setbounds(10, 32, 109, 23); panel.add(rdbtnadd); final jradiobutton rdbtnminus = new jradiobutton("minus 2"); rdbtnminus.setbounds(10, 58, 109, 23); panel.add(rdbtnminus); buttongroup group = new buttongroup(); group.add(rdbtnadd); group.add(rdbtnminus); jpanel panel_1 = new jpanel(); panel_1.setbounds(97, 0, 349, 262); getcontentpane().add(panel_1); panel_1.setlayout(null); panel_1.setbackground(color.blue); final jlabel label = new jlabel("0"); label.sethorizontalalignment(swingconstants.center); label.setbounds(67, 0, 260, 262); label.setfont(new font("arial", font.bold + font.italic, 45)); panel_1.add(label); jbutton btnsubmit = new jbutton("submit"); btnsubmit.setbounds(10, 88, 88, 23); panel.add(btnsubmit); btnsubmit.addactionlistener(new actionlistener() { public void actionperformed(actionevent e){ if (rdbtnadd.isselected()) label.settext("2"); if (rdbtnminus.isselected()) label.settext("-2"); } } ); } public static void main(string[] args) { twopaneldesign = new twopaneldesign(); a.setsize(500,400); a.setvisible(true); a.setdefaultcloseoperation(exit_on_close); } }
you must retain previous result:
public void actionperformed(actionevent e){ int v = integer.parseint(label.gettext()); if (rdbtnadd.isselected()){ v += 2; } else { v -= 2; } label.settext(integer.tostring(v)); }
Comments
Post a Comment