当前位置: 首页 > 知识库问答 >
问题:

使用HashMap键(字符串)作为其显示的JList?

夏奕
2023-03-14
public class Contact {

    private String name;
    private String phoneNumber;
    private String email;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
import java.util.HashMap;
import java.util.Map;

public class Book {

    private Map<String, Contact> addressbook = new HashMap<String, Contact>();

    public Map<String, Contact> getAddressbook() {
        return addressbook;
    }

    public void setAddressbook(Map<String, Contact> addressbook) {
        this.addressbook = addressbook;
    }

}
import java.awt.BorderLayout;

public class UserInterface extends JPanel implements ActionListener {

    private static final long serialVersionUID = 2161244209167568887L;

    // Contact list display
    JList contactList;

    // Menu bar and accompanying menu items
    private JMenuBar menuBar;
    private JMenu menu;
    private JMenuItem newContactMenuButton;
    private JMenuItem exitAppMenuButton;

    // Buttons
    private JButton newContactButton;
    private JButton openContactButton; 
    private JButton deleteContactButton; 

    // Panels to place components into
    private JPanel mainPanel;
    private JPanel buttonPanel; 

    // For message dialogs
    private JFrame messageDialog;

    public UserInterface() {

        // Add the JList
        contactList = new JList(new ContactListModel()); // ??

        // Creating the menu bar and its items
        // Adding ActionListeners to the menu buttons
        menuBar = new JMenuBar();
        menu = new JMenu("File");
        newContactMenuButton = new JMenuItem("New Contact");
        exitAppMenuButton= new JMenuItem("Exit");
        newContactMenuButton.addActionListener(this);
        exitAppMenuButton.addActionListener(this);
        menu.add(newContactMenuButton);
        menu.add(exitAppMenuButton);
        menuBar.add(menu);

        // Creating the Buttons
        // Adding ActionListeners to the buttons
        newContactButton = new JButton("New Contact");
        openContactButton = new JButton("Open Contact");
        deleteContactButton = new JButton("Delete Contact");
        newContactButton.addActionListener(this);
        openContactButton.addActionListener(this);
        deleteContactButton.addActionListener(this);

        // Creating the Panels with Grid Layouts
        mainPanel = new JPanel(new GridLayout());
        buttonPanel = new JPanel(new GridLayout(0, 1));

        // Adding components to the Panels
        mainPanel.add(contactList);
        buttonPanel.add(newContactButton);
        buttonPanel.add(openContactButton);
        buttonPanel.add(deleteContactButton);

        // Adding and aligning the Panels
        setBorder(BorderFactory.createEmptyBorder(45, 45, 45, 45));
        add(mainPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.EAST);

    }

    public void CreateAndShowUI() {
        JFrame frame = new JFrame("Addressbook Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new UserInterface());
        frame.setJMenuBar(this.menuBar);
        frame.pack();
        frame.setVisible(true);
    }


    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == newContactButton) {
            JOptionPane.showMessageDialog(messageDialog, "yaaaay!");
        }

        if (e.getSource() == openContactButton) {
            JOptionPane.showMessageDialog(messageDialog, "yaaaay!");
        }

        if (e.getSource() == deleteContactButton) {

            if(contactList.isSelectionEmpty()) {
                JOptionPane.showMessageDialog(messageDialog, "No contact selected.");

            } else if(!contactList.isSelectionEmpty()) {

                }
            }

        if (e.getSource() == newContactMenuButton) {
            JOptionPane.showMessageDialog(messageDialog, "yaaaay!");
        }

        if (e.getSource() == exitAppMenuButton) {
            System.exit(0);
        }
    }
}

final class ContactListModel extends AbstractListModel {

    Book book = new Book();
    Map<String, Contact> bookList = book.getAddressbook();

    public Object getElementAt(int keys) {
        keys = // ??
        return keys;
    }

    @Override
    public int getSize() {
        return bookList.size();
    }

}

以下是相关的更新代码位。正如用户carmickr所建议的,我使用DefaultListModel来处理地址簿HashMap中的数据

private DefaultListModel<Set<String>> model;
private JList<Set<String>> contactList;

然后在UserInterface构造函数内部:

// Create the DefaultListModel object
// Add the JList
model = new DefaultListModel<Set<String>>();
model.addElement(book.AB.keySet());
contactList = new JList<Set<String>>(model);

共有1个答案

贲铭
2023-03-14

我很难创建一个自定义列表模型,它从位于类书中的HashMap获取字符串键。

您不需要创建自定义模型。您只需将每个Contact对象添加到DefaultListModel。使用模型的意义在于模型包含所有的数据,并且您可以使用模型的方法来访问数据。

那么使JList工作的最简单方法就是在Contact对象中实现ToString()方法,以返回您希望在JList中看到的属性。

private DefaultListModel<Set<String>> model;
private JList<Set<String>> contactList;
// Create the DefaultListModel object
// Add the JList
model = new DefaultListModel<Set<String>>();
model.addElement(book.AB.keySet());
contactList = new JList<Set<String>>(model);
 类似资料:
  • 问题内容: 我试图理解为什么当用作Hashmap密钥时,将String和Stringbuilder / StringBuffer区别对待。让我通过以下插图使我的困惑更加清楚: 示例1,使用String: 上面的代码片段打印为“ 1”。 Example#2,使用StringBuilder(或StringBuffer): 上面的代码片段打印为“ 2”。 任何人都可以解释一下行为差异的原因。 问题答案:

  • 我看不出我的bug在哪里,我得到的是一个字符串数组,逐行包含以下值: 我想使用右边的字符串作为键将这些值放在HashMap中,并将值设置为ArrayList,这样,如果我请求“San Jose”的目的地,它将遍历列表并返回“旧金山”和“Anchorage”。我还没有设法使关键价值关系正常运作。方法运行完成时的原因。对于任何一个城市来说,其价值都将是其最后一次迭代(旧金山)的价值,返回纽约、檀香山和

  • 问题内容: 由于以下原因,我想使用不区分大小写的字符串作为HashMap键。 在初始化期间,我的程序使用用户定义的String创建HashMap。 在处理事件(在我的情况下为网络流量)时,我可能会在其他情况下收到String,但是我应该能够忽略HashMap中的来自我的情况而从HashMap 定位。 我遵循了这种方法 LookupCode.java 因此,我为每个事件创建一个CaseInsensi

  • 我在数据库表列中有手机号码,格式为,后面是,所以手机号码格式是这样的, 我有一个HashMap包含像这样的5个国家的国家代码, 我的豆子结构是这样的 现在,我的任务是从数据库表列中提取手机号码,并将其分成两部分,然后设置和,但国家代码长度(在地图键中)在3到4之间变化。这个检查可以通过使用和来完成,但我认为这不是正确的方法,那么解决这个问题的优雅(可能是签入)方法是什么呢?

  • 本文向大家介绍使用PHP显示动态突出显示的字符串,包括了使用PHP显示动态突出显示的字符串的使用技巧和注意事项,需要的朋友参考一下 该功能可能用途有限,但可以在您的标题中创建一些整洁的效果。它的工作原理是使用空格将字符串分成几小段,然后将其重新放回两部分。第一部分是正常的,但是第二部分将被包裹在span元素中。通过使用此功能,您可以通过将前半部分的样式与后半部分的样式不同来在标题中创建有趣的效果。

  • 问题内容: 具体来说,我正在尝试使用字符串任意过滤ORM。我已经尝试过exec和eval解决方案,但是我遇到了麻烦。下面的代码不起作用,但这是我知道如何解释我要去的地方的最好方法 问题答案: