Java事件:打印在按钮上存储名称的ArrayList按

时间:2015-11-14 04:49:45

标签: java events arraylist javafx

我有一个场景设置,允许用户在文本字段中输入名称,并在按下“添加员工”按钮后将名称存储在ArrayList中。我想在用户点击“打印名称”按钮后打印控制台中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));
        }
    }
}

1 个答案:

答案 0 :(得分: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;
}