为什么添加从ActionListener类调用的方法不会向集合中添加元素? Java的

时间:2016-07-26 12:54:15

标签: java swing

好的,这是我的GUI类(简称)

public class GUI {
private DataContainer dataContainer;

public GUI(DataContainer dataContainer){
    this.dataContainer = dataContainer;
    initGUI();
}

class RegisterListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e){
        String message;
        String firstName = registerPanelFirstNameTextField.getText();
        String lastName = registerPanelLastNameTextField.getText();
        String login = registerPanelLoginTextField.getName();
        String password = registerPanelPasswordTextField.getText();
        String adress = registerPanelAdressTextField.getText();
        Client client = new Client(firstName, lastName, login, password, adress);
        boolean registerCheck = dataContainer.registerClient(client);
        if (registerCheck) {
            message = "SUCCES!";
        } else {
            message = "FAILURE!";
        }
        JOptionPane.showMessageDialog(new JFrame(), message);
    }
}

这是我的DataContainer类(也简称):

public class DataContainer implements Subject {

public boolean workingStatus = true;
private List<Client> clientList = new ArrayList<>();
private List<Auction> auctionList = new ArrayList<>();
private List<Observer> observersList = new ArrayList<>();

public boolean registerClient(Client client) {
    String testLogin = client.getLogin();
    boolean isClientOnList = isClientOnList(testLogin);
    if (isClientOnList) {
        return false;
    } else {
        addClientToList(client);
        return true;
    }
}

private void addClientToList(Client client) {
    clientList.add(client);
    System.out.println(clientList);
}

这是我的问题 - 为什么从寄存器监听器调用的方法会给出空集合。 SysOut打印[null]。我已经使用JUnit和反射直接测试addClientToList(),它可以工作,但是单击按钮时却没有。是的,在我的课程中,我创建了新的DataContainer对象,并将其传递给GUI构造函数。

1 个答案:

答案 0 :(得分:0)

哦,对了!找到这个小家伙。问题在这里:

String login = registerPanelLoginTextField.getName();

应该是:

String login = registerPanelLoginTextField.getText();