这是框架的构造函数
public LibraryFrame() {
mod1 = new javax.swing.DefaultListModel();
books = new ArrayList<>();
Book b1 = new Book("book1","aAuthor","aPublisher","aIsbn",5,0);
books.add(b1);
mod1.addElement(b1.title);
initComponents();
}
这是听众
private void viewStockListValueChanged(javax.swing.event.ListSelectionEvent evt) {
int selected = viewStockList.getSelectedIndex();
String info = books.get(selected).title;
viewStockArea.insert(info,0);
}
当我为列表选择一个项目时,标题被插入到文本区域两次(最终是 book1book1 )
我已经尽可能多地研究了听众,但我找不到解决方案。感谢任何帮助
编辑:感谢kentcdodds,找到了解决方案。基本上,监听器创建了两个事件。查看他的答案以获得进一步的解释。这是正常工作的完成代码
private void viewStockListValueChanged(javax.swing.event.ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()){
int selected = viewStockList.getSelectedIndex();
String info = books.get(selected).title;
viewStockArea.insert(info,0);
}
}