1

我正在编写一个 Java (Swing) 应用程序,我正在尝试将内容存储在ArrayList. 我的计划是首先实例化一个类,在该类中设置一些变量,然后将该类添加到ArrayList.

所以我有这个类:

public class CollectionClass {

private ArrayList<OwnerClass> owners;

public CollectionClass() {

    owners = new ArrayList<OwnerClass>();

}


public void AddOwner(OwnerClass oc) {
    owners.add(oc);
}
 }

而这堂课:

public class OwnerClass {
public int id = 0;
public String name = "";
public String employeeNr="";
 }

现在在我的对话框中我有这个:

CollectionClass myCC;

public InvoerNewOwner(CollectionClass cc) {

            myCC = cc;
        btSave.setActionListener(new ActionListener() {
        OwnerClass oc = new OwnerClass();

        oc.name = txtOwner.getText();               <<<
        oc.employeeNr = txtEmployeeNr.getText();    <<<

        myCC.addOwner(oc);                            <<<

    });

在标有 <<< 的 3 行上出现错误。错误是“ error: <identifier> expected

我在这里做错了什么?

这不是创建对象集合的好方法吗?

4

1 回答 1

6
btSave.setActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        OwnerClass oc = new OwnerClass();

        oc.name = txtOwner.getText();
        oc.employeeNr = txtEmployeeNr.getText();

        myCC.addOwner(oc);
    }
});

你必须把你想做的所有事情都包装在一个actionPerformed事件中。

于 2013-02-10T14:12:18.930 回答