java - Cannot refer to a variable inside an inner class -
i'm still learning java bear me. trying write program click button named "yes" , increases variable y 1, , same button "no" on variable x. i'm using eclipse , windowbuilder pro , gives me error "cannot refer variable inside inner class"
here's code:
import javax.swing.japplet; import javax.swing.jbutton; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import javax.swing.jformattedtextfield; public class qa extends japplet { /** * create applet. */ public qa() { getcontentpane().setlayout(null); int y=0; int x=0; string s1 = string.valueof(y); jbutton btnyes = new jbutton("yes"); btnyes.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent arg0) { ++y; } }); btnyes.setbounds(135, 220, 85, 42); getcontentpane().add(btnyes); jbutton btnno = new jbutton("no"); btnno.addmouselistener(new mouseadapter() { @override public void mouseclicked(mouseevent e) { ++x; } }); btnno.setbounds(230, 220, 85, 42); getcontentpane().add(btnno); jformattedtextfield frmtdtxtfldvarible = new jformattedtextfield(); frmtdtxtfldvarible.settext(s1); frmtdtxtfldvarible.setbounds(147, 130, 157, 20); getcontentpane().add(frmtdtxtfldvarible); } }
thanks help!
declare x
, y
instance variables don't need declare them final local variables:
public class qa extends japplet { private int x = 0; private int y = 0; // ... }
Comments
Post a Comment