【问题标题】:Add multiple JTextField items to an ArrayList将多个 JTextField 项添加到 ArrayList
【发布时间】:2015-12-16 23:51:42
【问题描述】:

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

我正在创建一个 GUI 程序,该程序从用户那里获取联系人信息,并且每次单击 addButton 时都会创建一个联系人对象并将其添加到数组列表中,然后当单击 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();
}
}

【问题讨论】:

    标签: java user-interface arraylist


    【解决方案1】:

    每当您想将Contact 添加到您的list 时,您都需要创建一个 Contact。你可以用new Contact 做到这一点。此外,您不需要循环。每次event 触发时,您将one 添加到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("");
    }
    

    【讨论】:

    • 哦,好吧,我实际上是第一个,但它对我不起作用,所以这一定是我在添加联系人后“查看”联系人的方式?它似乎只打印输入到列表中的最后一个联系信息。关于如何打印出 JOptionPane 上的所有信息有什么问题吗?
    • 没关系,看起来我打印的是对象而不是实际列表。我认为这应该解决它。谢谢大家的帮助!!!
    • 尝试拨打list.toString()。您可能还希望构建一个大格式的String,但这确实是一个不同的问题(您应该提供一个MCVE)。
    • 为什么 list.toString() 而不仅仅是列表?另外,大格式字符串是什么意思?对于没有正确构建问题,我深表歉意,我总是尽量使其具体化。代码太多了吗?我想你们会想看看这一切以弄清楚发生了什么……
    • 此外,当信息打印在 JOption 窗口上时,会有一个 [ , ].... 开头是 [ ,然后是逗号,然后是另一个 ]。有什么办法可以摆脱它吗?
    猜你喜欢
    • 2015-12-16
    • 2017-10-11
    • 1970-01-01
    • 2012-04-14
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    相关资源
    最近更新 更多