2015-11-14 71 views
0

我有一個場景設置,允許用戶在文本字段中輸入名稱,並在按下「添加員工」按鈕後將名稱存儲在ArrayList中。一旦用戶點擊「打印名稱」按鈕,我想打印控制檯中ArrayList中的所有內容。我有方法來處理這兩個事件,但由於某種原因,只有存儲的姓氏正在打印。我確信每個名字都沒有被存儲並且確保它們是,一個循環需要在某個地方實現。Java事件:打印在按鈕上存儲名稱的ArrayList按

這是代碼。所有相應的庫都已導入。爲了簡潔,我已經排除了這些。

public class ComboBoxTest extends Application 
{ 
    ArrayList<String> empNames = new ArrayList<>(); 

    public static void main(String[] args) 
    { 
     Application.launch(args); 
    } 

    public void start(Stage primaryStage) 
    { 
     //hbox for label, textfield, and add button 
     HBox panes = new HBox(); 
     panes.setSpacing(10); 
     panes.setAlignment(Pos.CENTER); 

     //create label and textfield for employee name 
     Label empName = new Label("Employee name"); 
     TextField empTField = new TextField(); 
     Tooltip empTooltip = new Tooltip("Employee name"); 
     empTField.setTooltip(empTooltip); 

     //add employee button and properties 
     Button addEmpBtn = new Button("Add Employee"); 
     addEmpBtn.setAlignment(Pos.CENTER); 
     Tooltip addEmpToolTip = new Tooltip("Add employee"); 
     //addEmpBtn.setTooltip(addEmpToolTip); 

     //print employee names button 
     Button printEmpNamesBtn = new Button("Print Names"); 
     Tooltip printToolTip = new Tooltip("Print"); 
     //printEmpNamesBtn.setTooltip(printToolTip); 

     //hbox to line up the "print names" button 
     HBox printEmpOperation = new HBox(); 
     printEmpOperation.setSpacing(10); 
     printEmpOperation.setAlignment(Pos.BOTTOM_CENTER); 

     //add button to the hbox 
     printEmpOperation.getChildren().addAll(printEmpNamesBtn); 

     //actions for the buttons that are clicked 
     addEmpBtn.setOnAction(e -> empNames = addEmployeeNames(panes, empTField)); 

     printEmpNamesBtn.setOnAction(e -> printEmployeeNames(empNames)); 

     //add label, text field, and add button to the pane 
     panes.getChildren().addAll(empName, empTField, addEmpBtn); 

     BorderPane bPane = new BorderPane(panes); 
     BorderPane.setMargin(panes, new Insets(10, 10, 10, 10)); 
     bPane.setBottom(printEmpOperation); 
     //bPane.setMargin(panes, new Insets(10, 10, 10, 10)); 

     Scene scene = new Scene(bPane, bPane.getPrefWidth(), bPane.getPrefHeight()); 
     primaryStage.setTitle("Add Employees"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    //method should add each name entered in the textfield to an array 
    public static ArrayList<String> addEmployeeNames(HBox pane, TextField tf) 
    { 

     ArrayList<String> empNames = new ArrayList<>(); 
     empNames.add(tf.getText()); 

     tf.clear(); 

     return empNames; 
    } 

    //method should print every name stored in the array 
    public static void printEmployeeNames(ArrayList<String> e) 
    { 
     System.out.println("Employee Names:"); 
     System.out.println("---------------"); 

     for (int i = 0; i < e.size(); i++) 
     { 
      System.out.println(e.get(i)); 
     } 
    } 
} 

回答

0

的問題是,你實例調用addEmployeeNames一個新的ArrayList對象每次,該函數的最後一次通話後,很自然你就會擁有一個ArrayList只有一個入口,先添加的。 所以它接受員工作爲參數的ArrayList集合,您應該定義功能和數據添加到它,所以你可以這樣調用它

empNames = addEmployeeNames(panes, empTField, empNames) 

然後,它應該是

public static ArrayList<String> addEmployeeNames(HBox pane, TextField tf, ArrayList<String> employees) 
{ 
    employees.add(tf.getText()); 

    tf.clear(); 

    return employees; 
}