我正在尝试编写一个可以使用 GridLayout 输入任何大小的矩阵的函数,但我被卡住了,因为我找不到提取 JTextField 值以填充“mat”变量的合适方法(请参阅下面的 FIXME) .
/**
* @mat: matrix declared in main (e.g: mat[][] = new int[3][3];)
* @rows: number of matrix rows (e.g: int rows = 3;)
* @columns: number of matrix columns (e.g: int columns = 3;)
*/
public static int[][] inputMatrix(int[][] mat, int rows, int columns)
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(rows,columns));
for (int a=0; a<(rows*columns); a++)
{
panel.add(new JTextField(a));
}
if (JOptionPane.showConfirmDialog(null, panel, "Enter the matrix", JOptionPane.OK_CANCEL_OPTION)
== JOptionPane.OK_OPTION)
{
for(int a=0; a<(rows*columns); a++){
for(int b=0; b<rows; b++){
for(int c=0; c<columns; c++){
/* FIXME: find how to extract JTextField values. */
mat[b][c] = JTextField.a.getText();
}
}
}
}
return mat;
}
在此先感谢您的帮助!