Java: input a matrix using GridLayout

前端 未结 2 1207
忘掉有多难
忘掉有多难 2020-12-07 04:57

I am trying to write a function that can input a matrix of any size using a GridLayout, but I\'m stuck since I can\'t find an appropriate way of extracting the JTextField va

2条回答
  •  一个人的身影
    2020-12-07 05:36

    Let's say you have a 3-row x 3-column grid. Since GridLayout adds by rows, then the 1st item of the second row would be the 4th item that you added to the grid. You could retrieve this item by calling panel.getComponent(3) (zero index so 4th item is at index 3).

    So - you could just use getComponent, doing a little math to figure out the right index based on the number of columns and the i,j coordinates in the matrix.

提交回复
热议问题