将多个JTextField项添加到ArrayList

时间:2015-09-20 03:46:38

标签: java user-interface arraylist

我有一个新问题,我希望得到一些指导。

我正在创建一个GUI程序,它接收来自用户的联系信息,每次点击addButton时,都会创建一个Contact对象并将其添加到数组列表中,然后在单击viewButton时显示所有信息。

我终于将JTextField信息添加到了ArrayList中,但我不知道如何添加多个联系信息。我添加了firstNameInput.setText(“”);在输入第一个联系信息以清除屏幕但同时清除ArrayList之后。我想我需要某种循环但是我创建的循环最终会创建一个无限循环。这一切都在ActionPerformed方法中... 我理解为什么它是一个无限循环,但这是我第一次使用ArrayList,所以我感到困惑。

非常感谢指向正确方向的指针。

public class InputForm extends JFrame {
    private JPanel panel;
    private JLabel firstNameLabel;
    private JLabel lastNameLabel;
    private JLabel phoneNumLabel;
    private JLabel emailLabel;
    private JTextField  firstNameInput;
    private JTextField  lastNameInput;
    private JTextField  phoneInput;
    private JTextField  emailInput;
    private JButton addButton;
    private JButton viewButton;
    private String fn;
    private String ln;
    private String ph;
    private String em;
    private List<Contact> list = new ArrayList<Contact>();

public InputForm() {
    int windowWidth = 650;
    int windowHeight = 550;

    //Window title
    setTitle("CONTACT FORM");
    //Set window size.
    setSize(windowWidth, windowHeight);
    //Exit on close 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Buld the panel and add it the the JFrame.
    buildPanel();
    //Add the panel to the frames content pane
    add(panel);
    //Display the window.
    setVisible(true);   
}

private void buildPanel() {

    panel = new JPanel();
    addButton = new JButton("Add Contact");
    viewButton = new JButton("View Contacts");

    firstNameLabel = new JLabel("First Name");
    firstNameInput = new JTextField(10);
    lastNameLabel = new JLabel("Last Name: ");
    lastNameInput = new JTextField(10);
    phoneNumLabel = new JLabel("Phone Number: ");
    phoneInput = new JTextField(10);
    emailLabel = new JLabel("Email: ");
    emailInput = new JTextField(10);

    /*Add labels, text fields and buttons to the panel*/
    panel.add(firstNameLabel);
    panel.add(firstNameInput);
    panel.add(lastNameLabel);
    panel.add(lastNameInput);
    panel.add(phoneNumLabel);
    panel.add(phoneInput);
    panel.add(emailLabel);
    panel.add(emailInput);
    panel.add(addButton);
    panel.add(viewButton);

    theHandler handler = new theHandler();
    addButton.addActionListener(handler);
    viewButton.addActionListener(handler);
}


private class theHandler implements ActionListener {

   public void actionPerformed(ActionEvent event) {
        fn = firstNameInput.getText();
        ln = lastNameInput.getText();
        ph = phoneInput.getText();
        em = emailInput.getText();
        Contact con = new Contact(fn, ln, ph, em);

    while(event.getSource() == addButton) {
        list.add(con);
        fn = firstNameInput.getText();
        ln = lastNameInput.getText();
        ph = phoneInput.getText();
        em = emailInput.getText();
        list.add(con);  
        firstNameInput.setText("");
        lastNameInput.setText("");
        phoneInput.setText("");
        emailInput.setText("");
    }           
    if(event.getSource() == viewButton) {
        JOptionPane.showMessageDialog(null,"Contact Info:\n" + con);        
    }
}   
}
}

联系课程

public class Contact {
    private String fn;
    private String ln;
    private String ph;
    private String em;

public Contact(String fn, String ln, String ph, String em) { 
    this.fn = fn;
    this.ln = ln;
    this.ph = ph;
    this.em = em;
}
    public String getFirstName() {
        return fn;
    }
    public void setFirstName(String fn) {
        this.fn = fn;
    }   
    public String getLastName() {
        return ln;
    }
    public void setLastName(String ln) {
        this.ln = ln;
    }
    public String getPhone() {
        return ph;
    }
    public void setPhone(String ph) {
        this.ph = ph;
    }
    public String getEmail() {
        return em;
    }   
    public void setEmail(String em) {
        this.em = em;
    }

    public String toString() {
        return "First Name: " + getFirstName() + "\n" +
                "Last Name: " + getLastName() + "\n" +
                "Phone Number: " + getPhone() + "\n" +
                "Email: " + getEmail() + "\n";
    }

public static void main(String[] args) {
    new InputForm();
}
}

1 个答案:

答案 0 :(得分:6)

每当您想向Contact添加list时,您需要创建 Contact。您可以使用new Contact执行此操作。此外,您不需要循环。每次event触发时,您都会向List添加一个。像,

// Contact con = new Contact(fn, ln, ph, em);
// while(event.getSource() == addButton) {
if (event.getSource() == addButton) {
    fn = firstNameInput.getText();
    ln = lastNameInput.getText();
    ph = phoneInput.getText();
    em = emailInput.getText();
    Contact con = new Contact(fn, ln, ph, em);
    list.add(con); // <-- adds the Contact to the list.  
    firstNameInput.setText("");
    lastNameInput.setText("");
    phoneInput.setText("");
    emailInput.setText("");
}