如何使用java中的actionlistener按字母顺序对联系人列表进行排序?

blmhpbnm  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(354)

我有一个java任务,我应该按照从姓到名的字母顺序排列一个联系人列表,包括他们的电话号码。我一直在尝试使用诸如lastname、firstname、namelist和numberlist之类的东西,但仍然无法获得正确的信息,以便按照姓氏到名字的字母顺序对列表进行排序。如果我能得到一些帮助或提示来帮助我前进,那就太好了。我目前还使用actionlistener来帮助我对列表进行排序。
这里是构造函数代码的一部分。

public class ContactList extends JFrame implements ActionListener, ListSelectionListener {

    JMenuItem newMI, openMI, saveMI, saveAsMI, exitMI;
    JMenuItem searchMI, deleteMI, updateMI, newEntryMI, sortMI;
    JTextField lastName, firstName, phoneNumber;
    JList<String> listView;
    DefaultListModel<String> nameList = new DefaultListModel<String>();
    Vector<String> numberList = new Vector<String>();
    File currentFile = null;

    /**
     * Constructor
     */
    public ContactList() {
        super("Phone Contacts");          // set frame title
        setLayout(new BorderLayout());    // set layout

        // create menu bar
        JMenuBar menubar = new JMenuBar();
        setJMenuBar(menubar);

        // create file menu
        JMenu fileMenu = new JMenu("File");
        menubar.add(fileMenu);
        newMI = fileMenu.add(new JMenuItem("New"));
        newMI.addActionListener(this);
        openMI = fileMenu.add(new JMenuItem("Open"));
        openMI.addActionListener(this);
        fileMenu.addSeparator();
        saveMI = fileMenu.add(new JMenuItem("Save"));
        saveAsMI = fileMenu.add(new JMenuItem("Save As ..."));
        fileMenu.addSeparator();
        exitMI = fileMenu.add(new JMenuItem("Exit"));
        exitMI.addActionListener(this);

        // create edit menu
        JMenu editMenu = new JMenu("Edit");
        menubar.add(editMenu);
        updateMI = editMenu.add(new JMenuItem("Update"));
        updateMI.addActionListener(this);
        newEntryMI = editMenu.add(new JMenuItem("New Entry"));
        newEntryMI.addActionListener(this);
        deleteMI = editMenu.add(new JMenuItem("Delete"));
        deleteMI.addActionListener(this);
        editMenu.addSeparator();
        searchMI = editMenu.add(new JMenuItem("Search"));
        searchMI.addActionListener(this);
        sortMI = editMenu.add(new JMenuItem("Sort"));
        sortMI.addActionListener(this);

        // create phone list and controls
        JPanel listPanel = new JPanel(new BorderLayout());
        add(listPanel, BorderLayout.CENTER);
        JLabel label = new JLabel("Name List", JLabel.LEFT);
        listPanel.add(label, BorderLayout.NORTH);

        listView = new JList<String>(nameList);
        listView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        listView.addListSelectionListener(this);
        JScrollPane listScroller = new JScrollPane(listView);
        listPanel.add(listScroller, BorderLayout.CENTER);
        JPanel panel = new JPanel(new BorderLayout());
        add(panel, BorderLayout.WEST);
        JPanel editPanel = new JPanel(new GridLayout(6, 1));
        panel.add(editPanel, BorderLayout.NORTH);
        label = new JLabel("Last Name", Label.LEFT);
        editPanel.add(label);
        lastName = new JTextField();
        editPanel.add(lastName);
        label = new JLabel("First Name", Label.LEFT);
        editPanel.add(label);
        firstName = new JTextField();
        editPanel.add(firstName);
        label = new JLabel("Phone Number", Label.LEFT);
        editPanel.add(label);
        phoneNumber = new JTextField();
        editPanel.add(phoneNumber);
    }

下面是分配的actionlistener代码。我也不需要对电话号码进行分类。从姓到名。这些消息来源于我想弄清楚的sortmi问题。

// implementing ActionListener
    public void actionPerformed(ActionEvent event) {
        Object source = event.getSource();
        if(source == newMI) {
            nameList.clear();
            numberList.clear();
            currentFile = null;
            display(-1);
            setTitle("Phone Contacts");   // reset frame title
        }
        else if(source == openMI) {
            doOpen();
        }
        else if(source == deleteMI) {
            nameList.remove(getState());
            numberList.remove(getName());
        }
        else if(source == exitMI) {
            System.exit(0);
        }
        else if(source == updateMI) {
            int index = listView.getSelectedIndex();
            String name = lastName.getText().trim() + " " + firstName.getText().trim();
            String number = phoneNumber.getText().trim();
            if(index < 0) {  // add a new entry
                nameList.addElement(name);
                numberList.addElement(number);
                index = nameList.getSize()-1;
            }
            else {  // update an existing entry
                nameList.set(index, name);
                numberList.set(index, number);        
            }
            listView.setSelectedIndex(index);
            listView.ensureIndexIsVisible(index);
        }
        else if(source == newEntryMI) {
            listView.clearSelection();
            display(-1);
        }
        else if(source == searchMI) {
            String searchName = JOptionPane.showInputDialog(this,
                                "Please enter a name (last first) to search:");
            System.out.println("Name to search: " + searchName);
            nameList.contains(searchName);
            return;
        }
        else if(source == sortMI) {
            lastName.getText(); 
            firstName.getText();
        }
    }

我也在java中寻找了其他排序示例,但它不涉及actionlistener。

sh7euo9m

sh7euo9m1#

在类中使用copyinto方法 DefaultListModel ,以获取 nameList 作为一个数组。
在类中使用方法sort java.util.Arrays ,对数组进行排序。
创建新的 DefaultListModel 从排序的数组中。
设置模型 listView .

Object[] objs = nameList.copyInto();
Arrays.sort(objs, new Comparator<Object>() {
        @Override
        public int compare(Object o1, Object o2) {
            String s1 = o1 == null ? "" : o1.toString();
            String s2 = o2 == null ? "" : o2.toString();
            return s1.compareTo(s2);
        }
    });
nameList = new DefaultListModel(objs);
listView.setModel(nameList);

或者,您可以使用streamapi

Object[] objs = nameList.copyInto();
List<String> list = Arrays.stream(objs)
                          .map(obj -> obj == null ? "" : obj.toString())
                          .sorted()
                          .collect(Collectors.toList());
Vector<Object> v = new Vector<>(list);
nameList = new DefaultListModel(v);
listView.setModel(nameList);

相关问题