你必須這樣做,一種可能性是創建一個List
與for
循環的援助方法內,使該方法返回的許多方面它適合你在其他地方使用。
public List<JTextArea> createMultipleCells(int numOfCells) {
List<JTextArea> cells = new LinkedList<JTextArea>();
for(int i = 0; i < numOfCells; i++){
cells.add(new JTextArea(CELL_DIMENSIONS, CELL_DIMENSIONS));
}
return cells;
}
與陣列同樣的事情:
public JTextArea[] createMultipleCells(int numOfCells) {
JTextArea[] cells = new JTextArea[numOfCells];
for(int i = 0; i < numOfCells; i++){
cells[i] = new JTextArea(CELL_DIMENSIONS, CELL_DIMENSIONS);
}
return cells;
}
如果你已經知道如何手動創建一個細胞,你的下一步是瞭解如何使用循環。 – Voldemort