【发布时间】:2015-12-16 22:01:42
【问题描述】:
我删除了我的另一篇帖子,因为我被告知它不是具体的,并返回一个更具体的问题,这里是......
该程序应该接受联系信息(名字和姓氏,电话,电子邮件),然后单击添加按钮,将创建一个联系人对象并转到一个 ArrayList。然后,当单击查看按钮时,您将看到添加到其中的所有联系人。
我一直在观看教程并阅读有关如何使用 GUI 的书籍,并且能够创建一个包含所有必要内容的窗口来添加联系信息。
我的问题:我正在苦苦挣扎的是如何将此信息添加到数组列表中
在我弄清楚如何添加信息后,我将研究如何在单击查看按钮时查看它。
任何关于推动我前进的帮助或提示将不胜感激!
public class InputForm extends JFrame {
private static JPanel panel;
private static JLabel firstNameLabel;
private static JLabel lastNameLabel;
private static JLabel phoneNumLabel;
private static JLabel emailLabel;
private static JLabel contactMessage;
private static JTextField firstNameInput;
private static JTextField lastNameInput;
private static JTextField phoneInput;
private static JTextField emailInput;
private static JButton addButton;
private static JButton viewButton;
private String fn;
private String ln;
private String ph;
private String em;
private List<String> list = new ArrayList<String>();
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);
if(event.getSource() == addButton) {
list.add(con);
}
else if(event.getSource() == viewButton) {
JOptionPane.showMessageDialog(null,"Contact Info:\n" + con);
}
else if(event.getSource() == viewButton) {
JOptionPane.showMessageDialog(null, con);
}
}
}
public static void main(String[] args) {
new InputForm();
}
}
好的,这是我根据“满载鳗鱼的气垫船”的建议创建的一个联系人类。
public class Contact {
private String fn;
private String ln;
private String ph;
private String em;
private List<Contact> list = new ArrayList<Contact>();
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 fn;
}
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 swing user-interface arraylist