2016-10-13 49 views
0

我想創建一個使用Java GUI的日曆,我想創建一個方法來創建每個日期的單元格。有什麼辦法可以創建一個方法來創建一堆JTextAreas,而無需手動創建每個單獨的單元格?使用Java中的方法調用多個對象

由一個創建小區中的一個我做的:

public void createCell() { 
    cell1 = new JTextArea(CELL_DIMENSIONS, CELL_DIMENSIONS); 
} 
+2

如果你已經知道如何手動創建一個細胞,你的下一步是瞭解如何使用循環。 – Voldemort

回答

2

你必須這樣做,一種可能性是創建一個Listfor循環的援助方法內,使該方法返回的許多方面它適合你在其他地方使用。

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; 
     }