我不知道我错过了哪个概念,我编写了一个小项目,它使用java arraylist和GUI维护联系人。应用程序中有添加,删除,搜索,第一个,最后一个,上一个,下一个,退出,清除按钮。所有按钮除了第一个和最后一切都正常工作。我得到的问题是在函数Displayfirst **()
中这是代码
AddressBookDemo.java
pywhois
PersonDAO.Java
public void actionPerformed (ActionEvent e){
if (e.getSource () == jbbSave){
savePerson();
clear();
}
else if (e.getSource() == jbnDelete){
deletePerson();
clear();
}
else if (e.getSource() == jbnFirst){
displayFirstRecord();
clear();
}
else if (e.getSource() == jbnLast){
displayLastRecord();
clear();
}
else if (e.getSource() == jbnSearch){
searchPerson();
}
else if (e.getSource() == jbnForward){
displayNextRecord();
}
else if (e.getSource() == jbnBack){
displayPreviousRecord();
}
else if (e.getSource() == jbnClear){
clear();
}
else if (e.getSource() == jbnExit){
System.exit(0);
}
}
public void searchPerson() {
name = jtfName.getText();
name = name.toUpperCase();
/*clear contents of arraylist if there are any from previous search*/
personsList.clear();
if(name.equals("")){
JOptionPane.showMessageDialog(null,"Please enter person name to search.");
}
else{
/*get an array list of searched persons using PersonDAO*/
personsList = pDAO.searchPerson(name);
if(personsList.size() == 0){
JOptionPane.showMessageDialog(null, "No records found.");
//Perform a clear if no records are found.
clear();
}
else
{
/*downcast the object from array list to PersonInfo*/
PersonInfo person = (PersonInfo) personsList.get(0);
// displaying search record in text fields
jtfName.setText(person.getName());
jtfAddress.setText(person.getAddress());
jtfPhone.setText(""+person.getPhone());
jtfEmail.setText(person.getEmail());
}
}
}
****
public void displayFirstRecord(){
// dec in recordNumber to display previous person info, already
//stored in personsList during search
if(pDAO.getpersonsList().size() == 0 ){
JOptionPane.showMessageDialog(null, "There are no records");
/*if user has reached the begining of results, disable back button*/
jbnForward.setEnabled(true);
jbnBack.setEnabled(false);
// inc by one to counter last dec
}else{
System.out.println(pDAO.getpersonsList().size());
personsList = pDAO.getpersonsList();
PersonInfo person=(PersonInfo)personsList.get(0);
// displaying search record in text fields
jtfName.setText(person.getName());
jtfAddress.setText(person.getAddress());
jtfPhone.setText(""+person.getPhone());
jtfEmail.setText(person.getEmail());
//jbnFirst.setEnabled(false);
}
}
public void displayLastRecord(){
// dec in recordNumber to display previous person info, already
//stored in personsList during search
if(pDAO.getpersonsList().size()== 0 ){
JOptionPane.showMessageDialog(null, "There are no records");
/*if user has reached the begining of results, disable back button*/
jbnForward.setEnabled(true);
jbnBack.setEnabled(false);
// inc by one to counter last dec
}else{
jbnForward.setEnabled(true);
PersonInfo person = (PersonInfo) pDAO.getpersonsList().get(pDAO.getpersonsList().size()-1);
// displaying search record in text fields
jtfName.setText(person.getName());
jtfAddress.setText(person.getAddress());
jtfPhone.setText(""+person.getPhone());
jtfEmail.setText(person.getEmail());
}
}
********************
PersonInfo.Java
public class PersonDAO{
private ArrayList<PersonInfo> personsList;
*******************************
public ArrayList searchPerson(String name)
{
ArrayList<PersonInfo> personsList1=new ArrayList<>();
for (PersonInfo personsList2 : personsList) {
PersonInfo p = (PersonInfo) personsList2;
if(p.getName() == null ? name == null : p.getName().equals(name)){
personsList1.add(p);
break;
}
}
return personsList1;
}
答案 0 :(得分:2)
您正在调用assertRedirects
方法,该方法会清除clear()
的文字
JLable
...
else if (e.getSource() == jbnFirst){
displayFirstRecord();
clear();
}
else if (e.getSource() == jbnLast){
displayLastRecord();
clear();
}
.... public void displayFirstRecord(){
....
jtfName.setText(person.getName());
jtfAddress.setText(person.getAddress());
jtfPhone.setText(""+person.getPhone());
jtfEmail.setText(person.getEmail());
....
}
方法大致相同。
displayLastRecord()