I am programming a sudoku puzzle with each cell represented by a JTextField
, for 81 JTextField
objects in total. This arrangement leaves me wondering about how to efficiently clear the puzzle.
Right now I have this:
void clear() {
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
t5.setText("");
t6.setText("");
t7.setText("");
t8.setText("");
t9.setText("");
t10.setText("");
t11.setText("");
t12.setText("");
t13.setText("");
t14.setText("");
t15.setText("");
.
.
t81.setText("");
}
That's 81 .setText()
calls. Is there a better way to clear my puzzle?
Instead of t1
... t81
declare them like this:
JTextField[][] textField = new JTextField[9][9];
Refer to them like textField[2][2]
where this is the third row and third column ( [0]
is the first, remember!). Because this is a JTextField
I assume you are making UI, so for textField[i][j]
i
and j
could be rows/columns depending on how you put them on the screen.
To clear them all use a for loop. This is easy, so instead of answering this part I prepared a Google search for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.