gpt4 book ai didi

java - Swing 双击

转载 作者:行者123 更新时间:2023-12-01 17:25:06 25 4
gpt4 key购买 nike

当尝试通过双击从列表中选择项目时,它会为两个列表选择一次,但在单击选择后!!!

    listScrollPanel.setViewportView(categoryList);
subCategoryList.setModel(new javax.swing.AbstractListModel() {
String[] strings = {"Sub-category 1", "Sub-category 2", "Sub-category 3", "Sub-category 4", "Sub-category 5", "Sub-category 6"};
});
subCategoryList.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
infoBox(Integer.toString(evt.getClickCount()), subCategory);
if (evt.getClickCount() == 2) {
subCategoryList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
@Override
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
subCategoryListValueChanged(evt);
}
});
}
}
});
private void subCategoryListValueChanged(javax.swing.event.ListSelectionEvent evt) {

subCategory = (String) subCategoryList.getSelectedValue();
labelSC.setText(CAT_HEADER);
listScrollPanel.setViewportView(categoryList);
}
private static void infoBox(String Message, String location) {
JOptionPane.showMessageDialog(null, Message, location, JOptionPane.INFORMATION_MESSAGE);
}

如何让他只听双击?我有categoryList,双击后ListSelection将viewportView更改为subCategory,其工作原理相同!但他们在前两次双击后对单击使用react!

最佳答案

每次发生鼠标双击事件时,您都会添加一个新的 ListSelectionListener。其中每一项都会响应单击鼠标。您可以单独使用 MouseListener:

subCategoryList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() == 2) {
String selectedValue = subCategoryList.getSelectedValue();
// do stuff with selectedValue...
}
}
});

关于java - Swing 双击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15616934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com