我删除了我的其他职位,因为我被告知这是不特定的,并用更具体的问题,在这里不用回来....GUI添加的JTextField到一个ArrayList
程序应该采取联系信息(首先&姓氏,电话,电子邮件),然后你点击一个添加按钮,一个联系人对象将被创建并转到一个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();
}
}
你现在有什么问题?这是否编译?你还有其他问题吗? – markspace