我的联系人数据库中有以下代码,我需要学习如何将该数据提取到多个适当的 JTextField 以在 GUI 中显示。
ArrayList<Contact> contacts = new ArrayList<Contact>();
contacts.add(new Contact("Sally", "Smells", "2535551212", "ssmels@somedomain.edu"));
这就是我的 Contact.class 的样子。
import java.util.Arrays;
class Contact implements Comparable<Contact>
{
public Contact [] contacts;
public int contactNum;
public int currentContact;
public String search;
public String firstName;
public String lastName;
public String phone;
public String eMail;
public Contact()
{
super();
}
public Contact(String firstName, String lastName, String eMail, String phone)
{
super();
this.firstName = firstName;
this.lastName = lastName;
this.eMail = eMail;
this.phone = phone;
}
@Override
public int compareTo(Contact o) {
// TODO Auto-generated method stub
return 0;
}
/**
* @return the contacts
*/
public Contact[] getContacts() {
return contacts;
}
/**
* @param contacts the contacts to set
*/
public void setContacts(Contact[] contacts) {
this.contacts = contacts;
}
/**
* @return the contactNum
*/
public int getContactNum() {
return contactNum;
}
/**
* @param contactNum the contactNum to set
*/
public void setContactNum(int contactNum) {
this.contactNum = contactNum;
}
/**
* @return the currentContact
*/
public int getCurrentContact() {
return currentContact;
}
/**
* @param currentContact the currentContact to set
*/
public void setCurrentContact(int currentContact) {
this.currentContact = currentContact;
}
/**
* @return the search
*/
public String getSearch() {
return search;
}
/**
* @param search the search to set
*/
public void setSearch(String search) {
this.search = search;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the phone
*/
public String getPhone() {
return phone;
}
/**
* @param phone the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
/**
* @return the eMail
*/
public String geteMail() {
return eMail;
}
/**
* @param eMail the eMail to set
*/
public void seteMail(String eMail) {
this.eMail = eMail;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Contact [contacts=" + Arrays.toString(contacts)
+ ", contactNum=" + contactNum + ", currentContact="
+ currentContact + ", search=" + search + ", firstName="
+ firstName + ", lastName=" + lastName + ", phone=" + phone
+ ", eMail=" + eMail + "]";
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + contactNum;
result = prime * result + Arrays.hashCode(contacts);
result = prime * result + currentContact;
result = prime * result + ((eMail == null) ? 0 : eMail.hashCode());
result = prime * result
+ ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
result = prime * result + ((search == null) ? 0 : search.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Contact))
return false;
Contact other = (Contact) obj;
if (contactNum != other.contactNum)
return false;
if (!Arrays.equals(contacts, other.contacts))
return false;
if (currentContact != other.currentContact)
return false;
if (eMail == null) {
if (other.eMail != null)
return false;
} else if (!eMail.equals(other.eMail))
return false;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
if (search == null) {
if (other.search != null)
return false;
} else if (!search.equals(other.search))
return false;
return true;
}
}
这是我的 TestContacts.Java
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import com.sun.tools.javac.util.List;
public class TestContact extends JFrame
{
private JTextField txtFldSearch;
private JTextField txtFld_FirstName;
private JTextField txtFld_LastName;
private JTextField txtFld_Phone;
private JTextField txtFld_eMail;
public enum searchBy { First, Last, Phone, eMail };
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
TestContact frame = new TestContact();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestContact() {
ArrayList<Contact> contacts = new ArrayList<Contact>();
contacts.add(new Contact("Sally", "Smells", "2535551212", "ssmels@anyschool.edu"));
contacts.add(new Contact("Joe", "Smells", "2535551212", "jsmels@anyschool.edu"));
contacts.add(new Contact("Ally", "Bally", "2535551212", "aball@anyschool.edu"));
contacts.add(new Contact("Ah", "chu", "2535551212", "ahchu@anyschool.edu"));
contacts.add(new Contact("Molly", "Mocks", "2535551212", "mmocks@anyschool.edu"));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 393, 192);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
JMenuItem mntmOpen = new JMenuItem("Open");
mntmOpen.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// open data here
}
});
mnFile.add(mntmOpen);
JMenuItem mntmSave = new JMenuItem("Save");
mntmSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// save data here
System.out.println("prentent to save.\n\nSaving..........");
}
});
mnFile.add(mntmSave);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
//file exit
System.out.println("prentent to save.\n\nSaving..........");
System.exit(0);
}
});
mnFile.add(mntmExit);
JLabel lblSearch = new JLabel("Search: ");
menuBar.add(lblSearch);
txtFldSearch = new JTextField();
txtFldSearch.setToolTipText("Enter search criteria");
menuBar.add(txtFldSearch);
txtFldSearch.setColumns(10);
JLabel lblBy = new JLabel("By: ");
menuBar.add(lblBy);
JComboBox ddlSearchBy = new JComboBox();
ddlSearchBy.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent arg0) {
searchBy enumval = searchBy.valueOf(arg0.getItem().toString());
switch (enumval.ordinal() + 1) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
default:
break;
}
}
});
ddlSearchBy.setToolTipText("Search by");
ddlSearchBy.setModel(new DefaultComboBoxModel(new String[] {"First", "Last", "Phone", "eMail"}));
menuBar.add(ddlSearchBy);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblFirstName = new JLabel("First name: ");
lblFirstName.setBounds(6, 20, 100, 16);
contentPane.add(lblFirstName);
JLabel lblLastName = new JLabel("Last name: ");
lblLastName.setBounds(6, 48, 100, 16);
contentPane.add(lblLastName);
JLabel lblPhone = new JLabel("Phone: ");
lblPhone.setBounds(6, 76, 100, 16);
contentPane.add(lblPhone);
JLabel lblEmail = new JLabel("eMail: ");
lblEmail.setBounds(6, 104, 100, 16);
contentPane.add(lblEmail);
txtFld_FirstName = new JTextField();
txtFld_FirstName.setToolTipText("First name");
txtFld_FirstName.setBounds(77, 14, 185, 28);
contentPane.add(txtFld_FirstName);
txtFld_FirstName.setColumns(10);
txtFld_LastName = new JTextField();
txtFld_LastName.setToolTipText("Last name");
txtFld_LastName.setBounds(77, 42, 185, 28);
contentPane.add(txtFld_LastName);
txtFld_LastName.setColumns(10);
txtFld_Phone = new JTextField();
txtFld_Phone.setToolTipText("Phone number");
txtFld_Phone.setBounds(50, 70, 212, 28);
contentPane.add(txtFld_Phone);
txtFld_Phone.setColumns(10);
txtFld_eMail = new JTextField();
txtFld_eMail.setToolTipText("eMail address");
txtFld_eMail.setBounds(50, 98, 212, 28);
contentPane.add(txtFld_eMail);
txtFld_eMail.setColumns(10);
JButton jbtn_fwd = new JButton(">>");
jbtn_fwd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// fwd index here
}
});
jbtn_fwd.setBounds(274, 71, 117, 29);
contentPane.add(jbtn_fwd);
JButton jbtn_bak = new JButton("<<");
jbtn_bak.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// Back index here
}
});
jbtn_bak.setBounds(274, 99, 117, 29);
contentPane.add(jbtn_bak);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0)
{
// add contact
}
});
btnAdd.setBounds(274, 15, 117, 29);
contentPane.add(btnAdd);
JButton btnDel = new JButton("Del");
btnDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
// delete contact
contacts.remove(index);
}
});
btnDel.setBounds(274, 43, 117, 29);
contentPane.add(btnDel);
}
/**
* @return the txtFldSearch
*/
public JTextField getTxtFldSearch() {
return txtFldSearch;
}
/**
* @param txtFldSearch the txtFldSearch to set
*/
public void setTxtFldSearch(JTextField txtFldSearch) {
this.txtFldSearch = txtFldSearch;
}
/**
* @return the txtFld_FirstName
*/
public JTextField getTxtFld_FirstName() {
return txtFld_FirstName;
}
/**
* @param txtFld_FirstName the txtFld_FirstName to set
*/
public void setTxtFld_FirstName(JTextField txtFld_FirstName) {
this.txtFld_FirstName = txtFld_FirstName;
}
/**
* @return the txtFld_LastName
*/
public JTextField getTxtFld_LastName() {
return txtFld_LastName;
}
/**
* @param txtFld_LastName the txtFld_LastName to set
*/
public void setTxtFld_LastName(JTextField txtFld_LastName) {
this.txtFld_LastName = txtFld_LastName;
}
/**
* @return the txtFld_Phone
*/
public JTextField getTxtFld_Phone() {
return txtFld_Phone;
}
/**
* @param txtFld_Phone the txtFld_Phone to set
*/
public void setTxtFld_Phone(JTextField txtFld_Phone) {
this.txtFld_Phone = txtFld_Phone;
}
/**
* @return the txtFld_eMail
*/
public JTextField getTxtFld_eMail() {
return txtFld_eMail;
}
/**
* @param txtFld_eMail the txtFld_eMail to set
*/
public void setTxtFld_eMail(JTextField txtFld_eMail) {
this.txtFld_eMail = txtFld_eMail;
}
/**
* @return the buttonGroup
*/
public ButtonGroup getButtonGroup() {
return buttonGroup;
}
/**
* @param buttonGroup the buttonGroup to set
*/
public void setButtonGroup(ButtonGroup buttonGroup) {
this.buttonGroup = buttonGroup;
}
/**
* @return the contentPane
*/
@Override
public JPanel getContentPane() {
return contentPane;
}
/**
* @param contentPane the contentPane to set
*/
public void setContentPane(JPanel contentPane) {
this.contentPane = contentPane;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TestContact [txtFldSearch=" + txtFldSearch
+ ", txtFld_FirstName=" + txtFld_FirstName
+ ", txtFld_LastName=" + txtFld_LastName + ", txtFld_Phone="
+ txtFld_Phone + ", txtFld_eMail=" + txtFld_eMail
+ ", buttonGroup=" + buttonGroup + ", contentPane="
+ contentPane + "]";
}
}