我正在尝试将矩阵的数字序列打印到 jtextfield 中。 我的 for 循环在控制台上工作时工作正常,但在文本字段上工作时不行。
private void btnperformAction(java.awt.event.ActionEvent evt) {
int [][] boardOne = defineBoard(); //initializes matrix
fillBoard(boardOne); //adds integers values to the matrix
int i,j;
for(i = 0; i < boardOne.length; i++){
for(j = 0; j < boardOne[i].length; j++){
txtField.setText("" + boardOne[i][j]);
}
}
这些是我在 jtextfield 上需要的控制台上打印的值
8 19 39 63 66
6 21 32 61 72
4 26 -1 64 74
5 20 33 55 76
14 16 35 51 80
文本字段只会输出最后一个数字,在本例中为 80
。这是为什么?
非常感谢任何帮助。
最佳答案
您需要连接先前的文本字段值。当前您正在设置当前位置环所指的值。将您的代码更改为如下所示
txtField.setText(txtField.getText() + boardOne[i][j]);
关于java - 如何通过 jtextfield 迭代和打印矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56000587/