【发布时间】:2017-09-21 23:32:49
【问题描述】:
我已经自学了几个月的 Java 并且遇到了挑战。我正在制作一个联系人列表应用程序。我选择使用 HashMap<String, Contact> 作为我的联系人存储空间。我遇到的挑战是我对 Swing 的不熟悉。我第一次尝试使用 JList。我已经让 JList 可以处理普通的字符串数组,但现在我想使用 HashMap 中的 Key 值来显示 JList。
我在其他地方读到自定义ListModel 可以,但我没有找到任何具体的东西。 oracle 文档How to Use Lists 在其示例中使用了 DefaultListModel。我读过使用 AbstractListModel 或 ListModel 是朝着正确方向迈出的一步。到目前为止,主要有三个类:
班级联系人
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;
}
}
类用户界面 这是我在创建自定义列表模型时遇到困难的地方,该模型从位于类 Book 中的 HashMap 中获取字符串键。
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);
【问题讨论】:
-
请将部分答案添加到下面的答案中,而不是问题中。