Swing中的鼠标和键盘侦听器 [英] mouse and keyboard listeners in Swing

查看:125
本文介绍了Swing中的鼠标和键盘侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此侦听器95%的时间有效:

This listener works 95% of the time:

    messagesJList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
        public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
            messagesJListValueChanged(evt);
        }
    });

但是,它有时会在不方便的时间进行注册。毫无疑问,我的错误处理是潜在的问题。话虽这么说,是否有一个替代的侦听器,它聚集了各种鼠标和键盘侦听器,但仅聚集那些事件?

however, it will sometime register at an inconvenient time. No doubt, my error handling is the underlying problem. That being said, is there an alternative listener which aggregates the various mouse and keyboard listeners, but only those events?

推荐答案

我只是组合:

private void messagesJListKeyReleased(java.awt.event.KeyEvent evt) {
    userSelectedRow();
}

private void messagesJListMouseReleased(java.awt.event.MouseEvent evt) {
    userSelectedRow();
}

所以只有当用户实际用鼠标或键盘单击时,方法userSelectedRow ()被调用。

so that only if the user actually clicks with mouse or keyboard is the method userSelectedRow() invoked.

这篇关于Swing中的鼠标和键盘侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆