java - Updating variables inside labels -
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class testswinglisteners1 { private static int cnt1; private static int cnt2; public static void main(string[] args) { jframe fr1 = new jframe("swing window"); container cp; jbutton bt1; jbutton bt2; cnt1 = 0; cnt2 = 0; string scr = null; string wnr = null; jbutton btok, btcancel; fr1.setdefaultcloseoperation(jframe.exit_on_close); fr1.setsize(300, 200); fr1.setresizable(false); cp = fr1.getcontentpane(); cp.setlayout(new gridlayout(5,1)); btok = new jbutton("ac milan"); btcancel = new jbutton("real madrid"); jlabel lbl1 = new jlabel("result: " + cnt1 + "x" + cnt2); jlabel lbl2 = new jlabel("last scorer: " + scr); jlabel lbl3 = new jlabel("winner: " + wnr); cp.add(btok); cp.add(btcancel); cp.add(lbl1); cp.add(lbl2); cp.add(lbl3); //lbl1.settext(displaytext); btok.addactionlistener(new actionlistener(){ public void actionperformed(actionevent ae) { //string displaytext = "" + 1; cnt1++; } }); btcancel.addactionlistener(new actionlistener(){ public void actionperformed(actionevent ae) { cnt2++; } }); fr1.show(); }
when press btok
button, want cnt1
increment , same btcancel
- increment cnt2
when it's pressed.
how it?
public class testswinglisteners1 implements actionlistener { // ... btok = new jbutton("ac milan"); btok.setactioncommand("ok"); btokaddactionlistener(this); btcancel = new jbutton("real madrid"); btcancel.setactioncommand("cancel"); btcancel.addactionlistener(this); // ... public void actionperformed(actionevent e) { if(e.getactioncommand().equals("ok")) { lbl1.settext("result: " + ++cnt1 + "x" + cnt2); } else if(e.getactioncommand().equals("cancel")) { lbl1.settext("result: " + cnt1 + "x" + ++cnt2); } } }
Comments
Post a Comment