How to access boolean array Java -
i have following constructor defines board
, checks if of 3 jbutton
s have been clicked:
timer timer = new timer(500, this); private boolean[][] board; private boolean isactive = true; private int height; private int width; private int multiplier = 40; jbutton button1; jbutton button2; jbutton button3; public board(boolean[][] board) { this.board = board; height = board.length; width = board[0].length; setbackground(color.black); button1 = new jbutton("stop"); add(button1); button1.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { isactive = !isactive; button1.settext(isactive ? "stop" : "start"); } }); button2 = new jbutton("random"); add(button2); button2.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { this.board = randomboard(); } }); button3 = new jbutton("clear"); add(button3); button3.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { this.board = clearboard(); } }); }
but returns error:
exception in thread "main" java.lang.error: unresolved compilation problems: board cannot resolved or not field board cannot resolved or not field
why this? how access this.board
in constructor?
the problem caused trying access this.board
inside anonymous inner classes. since there no board
field defined, causes error.
for example this:
button2.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { this.board = randomboard(); } });
in order able use board
variable inside anonymous inner classes either need remove this
or use board.this.board
(if want more explicit).
Comments
Post a Comment