所以我已经在这个项目的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);
}
...发布于 2014-04-30 09:44:45
建议
int index = nameList.getSelectedIndex();?看起来选定的索引不会在这里给你任何有用的信息。if(nameList.equals(searchName)){。这样做将打印出else语句,更好的做法是使用Swing库组件,而不是AWT。https://stackoverflow.com/questions/23378204
复制相似问题