1

如何清除 6x6“表”,以便清除其中的任何内容?(我已经用 ActionListener ...等制作了清除按钮)

        //other code above that creates window, below is the code that creates the table I need to clear

         square = new JTextField[s][s];
    for (int r=0; r!=s; r++) {
        symbols[r] = new JTextField();
        symbols[r].setBounds(35+r*35, 40, 30, 25);
        win.add(symbols[r], 0);
        for (int c=0; c!=s; c++) {
            square[r][c] = new JTextField();
            square[r][c].setBounds(15+c*35, 110+r*30, 30, 25);
            win.add(square[r][c], 0);
        }
    }
    win.repaint();
}
4

3 回答 3

1

循环遍历数组并将每个元素设置为空。您可以使用java.utils.Arrays实用程序类使事情变得更干净/更整洁。

for( int i = 0; i < square.length; i++ )
   Arrays.fill( square[i], null );
于 2012-10-18T02:12:32.663 回答
0

就像是...

for (int index = 0; index < square.length; index++) {
    square[index] = null;
}
square = null;

会做更多的伎俩(实际上最后一行通常就足够了)......

如果你真的偏执...

for (int index = 0; index < square.length; index++) {
    for (int inner = 0; inner < square[index].length; inner++) {
        square[index][inner] = null;
    }
    square[index] = null;
}
square = null;
于 2012-10-18T01:16:29.067 回答
0

这是一条线解决方案:

Arrays.stream(square).forEach(x -> Arrays.fill(x, null));
于 2019-10-23T01:12:19.690 回答