【发布时间】:2014-04-30 01:35:50
【问题描述】:
所以我已经在这个项目 PhoneBook.java 程序上工作了一段时间。该程序打开一个 .txt 文件并将其导入到按姓氏、名字排序的列表中。我正在尝试编写一个打开一个窗口的搜索功能,要求您输入一个名称,然后单击“确定”它应该选择搜索的索引。我不明白为什么我的 searchMI 以下代码不起作用。我很感激你能给我的任何帮助。
public class PhoneBook extends Frame implements ActionListener, ItemListener {
MenuItem newMI, openMI, saveMI, saveAsMI, exitMI;
MenuItem searchMI, deleteMI, updateMI, newEntryMI, sortMI;
String fileName;
List nameList;
List numberList;
TextField lastName, firstName, phoneNumber;
// implementing ActionListener
public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if(source == newMI)
{
nameList.removeAll();
numberList.removeAll();
fileName = null;
display(-1);
setTitle("White Pages")
}
else if(source == searchMI)
{
String searchName = JOptionPane.showInputDialog(this,
"Please enter a name (last first) to search:");
System.out.println("Name to search: " + searchName);
int index = nameList.getSelectedIndex();
String name = lastName.getText().trim() + " " + firstName.getText().trim();
for(int i=0; i!=index; i++){
if(nameList.equals(searchName)){
nameList.select(index);
}
else
{
System.out.println("Error searching for the name: " + searchName);
}
...
【问题讨论】: