我刪除了我的其他職位,因爲我被告知這是不特定的,並用更具體的問題,在這裏不用回來....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