0

我正在为一个项目设计一个基本的电话簿。它具有三个类,Main(用于 GUI)、TelephoneDirectory(存储 TelephoneRecords 对象的对象)和 TelephoneRecords 类(存储每个记录对象的信息)。

需求状态:通过添加一个列表来扩展您的应用程序,该列表显示电话簿的完整当前内容,按姓名字母顺序排列。您将需要实现一个ListModel. 您可能想AbstractListModel在开始自己的实现之前学习课程。

唯一的问题是,我完全不知道如何扩展我的应用程序来实现这一点。我在网上搜索了一整夜,还没有找到办法做到这一点。我尝试将对象存储在 AbstractListModel 而不是 HashMap 中,但出现错误。我不完全知道使用什么或为什么使用它以及如何使用它。下一个要求(顺便说一句)是让 JList 在输入新数据时自动更新它,所以我想这与它有关吗?

无论哪种方式,如果有人可以提供帮助,那就太好了。我当前需要编辑的上一个版本的工作代码是:

主要的

public class Main extends JFrame implements ActionListener {

private static TelephoneDirectory directory = new TelephoneDirectory();
private JTextField nameField;
private JTextField numberField;
private JList contactList;

public Main() {
    setTitle("Telephone Directory");
    setLayout(new GridLayout(0,2));

    JLabel nameLabel = new JLabel("Name of Contact:");
    nameField = new JTextField(20);
    add(nameLabel);
    add(nameField);

    JLabel numberLabel = new JLabel("Number of Contact:");
    numberField = new JTextField(20);
    add(numberLabel);
    add(numberField);

    JButton enterButton = new JButton("Enter");
    JButton cancelButton = new JButton("Cancel");
    enterButton.addActionListener(this);
    cancelButton.addActionListener(this);
    add(enterButton);
    add(cancelButton);

    JLabel contactsLabel = new JLabel("Current Contacts:");
    contactList = new JList();
    add(contactsLabel);
    add(contactList);

    setVisible(true);
    pack();
}

public static void main(String[] args) {

    new Main();

}

@Override
public void actionPerformed(ActionEvent arg0) {
    JButton jb = (JButton) arg0.getSource();
    if (jb.getText().equals("Cancel")) {
        System.exit(0);
    } else {
        directory.addRecord(nameField.getText(), new TelephoneRecords(nameField.getText(), numberField.getText()));
        System.out.println("Added record for " + nameField.getText() + ": number is " + numberField.getText()   + ".");
    }
}

}

电话目录

public class TelephoneDirectory implements Iterable<TelephoneRecords> {

private HashMap records;

public TelephoneDirectory() {
    records = new HashMap<String, TelephoneRecords>();
}

public void addRecord(String name, TelephoneRecords newRecord) {
    records.put(name, newRecord);
}

public TelephoneRecords getRecord(String name) {
    return (TelephoneRecords) records.get(name);
}

public void getDirectory() {
    System.out.println("Telephone Directory:");
    records.values().iterator();
}

@Override
public Iterator<TelephoneRecords> iterator() {
    return records.values().iterator();
}

}

电话记录

public class TelephoneRecords {

private String name;
private String number;

public TelephoneRecords(String name, String number) {
    this.name = name;
    this.number = number;
}

public String getName() {
    return name;
}

public String getNumber() {
    return number;
}

@Override
public String toString() { 
    return "The phone number of " + name + " is " + number + ".";
}

}
4

1 回答 1

4

You may be trying to do too much with inheritance. Rather than using an AbstractListModel in place of your HashMap, consider creating a class that extends AbstractListModel and that holds the TelephoneDirectory class with its HashMap as the nucleus of the AbstractListModel's data. This is called extending a class by composition rather than by inheritance.

Edit: Also consider using a TreeMap rather than a HashMap so as to be able to retrieve your names and telephone records in name order. You'll also need to give your TelephoneDirectory class a getElementAt(int index) and a getSize() method to allow it to be used within the AbstractListModel class.

于 2012-03-09T01:58:32.373 回答