我一直在自我學習Java的一兩個月,並已運行到一個挑戰。我正在製作聯繫人列表應用程序。我選擇使用HashMap<String, Contact>
作爲我儲存的聯繫人。我遇到的挑戰是我對Swing的陌生感。我第一次嘗試使用JList。我有JList使用正常的字符串數組,但現在我想使用HListMap中的值作爲JList顯示的值。JList中使用的HashMap鍵(字符串)作爲其顯示?
我看過其他地方自定義ListModel
會做,但我並沒有發現任何具體。神諭文件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;
}
}
類的UserInterface 這是我遇到的困難創建自定義列表模型,它從位於Book類的HashMap中獲取String鍵。
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);
請加上答案的部分下面的答案,而不是問題。 –